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?
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.
NET Standard (2.0. 7 and beyond). In fact, I can use Log4Net alongside the default logging API for . NET Core: Microsoft.
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" />
Log4Net Configuration in WCF should be as below:
This will add log4net package to references, double check.
Add the following line in AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.xml", Watch = true)]
Create log4net.xml in application root, and add configurations for logging, as below:
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);
Use _log as follow:
catch (System.Exception ex) _log.Error(ex);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With