Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup log4net so that it works on a wcf service?

Tags:

c#

log4net

I'm working with a solution on visual studio 2008, .net framework 3.5, windows 7. I've created a log4net library that writes to a txt file and want to use it on several projects on my solution and also on a wcf service that's in the same solution which is running locally from visual studio.

I start the program from a console application on the solution. And the console app calls the other projects and those projects use the log4net. At this level in the workflow the log is writing fine. The problem comes when the wcf service is called. The wcf service uses the log, but log4net doesn't write to the file.

On the console project I have this:

On the AssemblyInfo.cs:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

On the App.config I have the log4net configuration.

On Program.cs, on the Main method I have:

LogManager.GetLogger("Initialise log4net from the current assembly attributes");

On the WCF Service I have this:

The same on the AssemblyInfo.cs as the console project.

Same on Web.config as the App.config on the console project.

On the constructor of the Service1.svc I have:

LogManager.GetLogger("Initialise log4net from the current assembly attributes");

This is how my App.config and Web.Config loos like:

inside the configSections tag:

 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>

and inside the main configuration tag:

 <log4net>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
      <file value="C:\test3.txt"/>
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d [%t] %-5p %c %m%n" />
      </layout>
    </appender>
  </log4net>

How can I solve this?

like image 227
pyram Avatar asked Nov 06 '14 22:11

pyram


People also ask

Where do I put log4net in web config?

Now, add the section "<log4net></log4net>" after the <configSections/> element in your app. config file. Next, inside the "<log4net></log4net>" section, place the configuration details as shown in the code snippet given below. That's all you need to do to configure log4net.

Does log4net support .NET core?

NET Standard (2.0. 7 and beyond). In fact, I can use Log4Net alongside the default logging API for . NET Core: Microsoft.


2 Answers

Do you have an example of the XmlConfigurator so I can configure the logging?

var logpath = HostingEnvironment.MapPath("~/web.config");
var fileInfo = new FileInfo(logpath);

if (fileInfo.Exists == false)
{
    throw new InvalidOperationException("Can't locate the web.config file");
}

log4net.Config.XmlConfigurator.ConfigureAndWatch(fileInfo);

Note that only the overload of the ConfigureAndWatch method which takes a FileInfo can watch an app.config or web.config as it reads the .config file directly instead of using System.Configuration (which once it reads the file cannot re-read it).

The documentation explains this here.

Edit by @pyram: because both projects are logging to the same file it was necessary to add this line to the appender config of both projects:

<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
like image 114
stuartd Avatar answered Nov 03 '22 14:11

stuartd


Log4Net Configuration in WCF should be as below:

  1. Install Log4net nugget package in your project

This will add log4net package to references, double check.

  1. Add the following line in AssemblyInfo.cs

    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.xml", Watch = true)]

  2. Create log4net.xml in application root, and add configurations for logging, as below: enter image description here

  3. Add log property in service to use it in every method:

    private static readonly log4net.ILog _log = log4net.LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

  4. Use _log as follow:

    catch (System.Exception ex) _log.Error(ex);

like image 30
Avjol Sakaj Avatar answered Nov 03 '22 15:11

Avjol Sakaj