I am extremely confused about how to configure log4net in a Windows Service project. Add to that, that I'm a newbie with Visual Studio configurations. So, I've created this Windows Service application and I want to add log4net to it. All instructions are different and one answer said to include the following line to the assembly info. Another answer said to add it to the AssemblyInfo.cs (in your App_Code folder). So does this mean that I need to add a class called AssemblyInfo.cs in my sources folder and then just add this line in the constructor? I don't know what that means!
[assembly:log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
I've added the appSettings to my app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
Do I also need to create a log4net.config file?
I tried to follow instructions from this questions but there's just not enough information to make sense to me. I've spent hours looking for instructions that make sense. Can someone please tell me (at a high level) the steps to configure log4net for a Windows Service application? Which files I need to create; what configurations I need to add; I know I will need to create a logger class but the configuration has me completely confused.
---------------------------------EDIT----------------------------------
Step 1) My app.config looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message %exception%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
</configuration>
Step 2) I've added this the Program.cs main:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace MyAppService
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
log4net.Config.XmlConfigurator.Configure();
Step 3) I've added the reference to log4net
Step 4) In my classes where I am logging I've added this:
private static ILog logger = LogManager.GetLogger(typeof(Reader));
Step 4) I've added logging statements like this:
logger.Info("Data Read Completed Successfully.");
You can use app.config for your configuration: for a Windows service having a separate file is a preference rather than a necessity, as the service can watch the config file for changes.
Regarding the assembly directive anywhere, you can add it to any file as long as it's in the startup project - your service - though it's customary to add them to the existing AssemblyInfo.cs file.
You must also make a call to Log4net in your startup routine, as the documentation says in bold:
Using attributes can be a clearer method for defining where the application's configuration will be loaded from. However it is worth noting that attributes are purely passive. They are information only. Therefore if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked.
To do this, your service startup code must contain a line like:
LogManager.GetLogger("initialise logging system");
Alternatively, you could drop the attributes and just call XmlConfigurator.ConfigureAndWatch()
in the startup program instead, which will by default load and watch the app.config file. Again, a matter of preference whether to use that or the assembly attributes to load the configuration.