mardi 27 mai 2014

log4net in class library


Vote count:

0




I am creating a class library that will be reused among many new projects in our company. I need to add log4net logging to both this library and the projects that use the library.


I have the following in my project using the library:


log4net.config (using DatePattern to create a log file named Debug_2014-05.txt)



<?xml version="1.0" encoding="utf-8" ?>
<log4net xsi:noNamespaceSchemaLocation="log4net.xsd" xmlns:xsi="http://ift.tt/ra1lAU">

<!-- Setup Rolling Log File to log all information -->
<appender name="DebugFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="${ProgramData}\\ConsoleExample\\log\\Debug" />
<param name="AppendToFile" value="true"/>
<param name="RollingStyle" value="Date"/>
<param name="DatePattern" value="_yyyy-MM.\tx\t"/>
<param name="StaticLogFileName" value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>

<root>
<level value="ALL" />
<appender-ref ref="DebugFileAppender" />
</root>

</log4net>


AssemblyInfo.cs



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


App.config



<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<!-- log4net section -->
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false" />
</configSections>

<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>


In any file that implements logging:



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


If I don't add anything to the library project's AssemblyInfo.cs files then logging does nothing in the library files that call the log method (the project using the library does log events properly).


If I add [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)] to the library's AssemblyInfo.cs file I get an error message of:



log4net:ERROR [RollingFileAppender] Unable to acquire lock on file C:\ProgramData\ConsoleExample\log\Debug_2014-05.txt. The process cannot access the file 'C:\ProgramData\Delphia Consulting\ConsoleExample\log\Debug_2014-05.txt' because it is being used by another process.


Surprisingly though log4net creates another file called Debug_201405.txt_2014-05.txt. Is there a way to get the library files to log to the Debug_2014-05.txt file (same file that the project files use for logging)? Is the second file created some kind of default that log4net falls back to if it cannot get access to the specified file?



asked 27 secs ago






Aucun commentaire:

Enregistrer un commentaire