How to configure log4net so that it works on the wcf service?

I am working on a solution on Visual Studio 2008, .NET Framework 3.5, Windows 7. I created a log4net library that writes to a txt file and wants to use it in several projects on my solution, as well as on the wcf service, which is in the same solution that runs locally from a visual studio.

I run the program from the console application in the solution. And the console application invokes other projects, and these projects use log4net. At this level, the journal writes excellently in the workflow. The problem occurs when the wcf service is called. The wcf service uses logging, but log4net does not write to the file.

In a console project, I have the following:

In AssemblyInfo.cs:

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

In App.config, I have a log4net configuration.

In Program.cs by the main method, I:

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

In the WCF service, I have the following:

Same thing in AssemblyInfo.cs as in a console project.

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

In the constructor of Service1.svc, I have:

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

This is how my App.config and Web.Config applications look 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?

+6
source share
1 answer

Do you have an XmlConfigurator example, so I can configure 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 that FileInfo accepts can watch app.config or web.config, because it reads the .config file directly, and does not use System.Configuration (which after reading the file, read it) .

The documentation explains this here .

Edit with @pyram: since both projects are registered in the same file, you need to add this line to the append configuration of both projects:

 <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> 
+3
source

Source: https://habr.com/ru/post/977855/


All Articles