unable to get log4net working with .net windows service

Please note that when the process is run as Windows Service, Environment.CurrentDirectory will be “C:\Windows\system32” So if you put the log4net configuration file (log4net.config) next to your *.exe, you can use the following code to configure log4net. var assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); XmlConfigurator.Configure(new FileInfo(Path.Combine(assemblyFolder, “log4net.config”)));

Writing logs to file

Here is a complete step-by-step guide to adding Log4Net to your project, under Visual Studio 2012 and .NET 4.5. Add a new C# console app to your solution. Select Tools >> Library Package Manager >> Manage NuGet Packages For Solution and search for log4net. Install it, and select which project(s) you want to add the … Read more

Configure log4net to send errors to different appenders based on level

This can be done using the threshold or filter elements within the appender. Note that the threshold can be directly under the appender, where it acts as an inclusive filter, or under a evaluator e.g. <evaluator type=”log4net.Core.LevelEvaluator”> <threshold value=”ERROR”/> </evaluator> where it acts as an inclusive filter for skipping buffering (immediate output), where applicable. Full … Read more

How to configure log4net to print to console in debug mode

You need to have both appenders declared in your <root> section. Configure your log4net like this: <log4net> <appender name=”Console” type=”log4net.Appender.ConsoleAppender”> <layout type=”log4net.Layout.PatternLayout”> <conversionPattern value=”%date %-5level: %message%newline” /> </layout> </appender> <appender name=”RollingFileAppender” type=”log4net.Appender.RollingFileAppender”> <file value=”Log.txt” /> <appendToFile value=”false” /> <rollingStyle value=”Size” /> <maxSizeRollBackups value=”10″ /> <maximumFileSize value=”50MB” /> <staticLogFileName value=”true” /> <layout type=”log4net.Layout.PatternLayout”> <conversionPattern value=”%date %-5level[%logger]: … Read more

log4net hierarchy and logging levels

This might help to understand what is recorded at what level Loggers may be assigned levels. Levels are instances of the log4net.Core.Level class. The following levels are defined in order of increasing severity – Log Level. Number of levels recorded for each setting level: ALL DEBUG INFO WARN ERROR FATAL OFF •All •DEBUG •DEBUG •INFO … Read more