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

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" />

Tags:

C#

Log4Net