Reputation: 4185
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://www.w3.org/2001/XMLSchema-instance">
<!-- 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?
EDIT
I found another post that explained how to implement log messages for log4net and found the following which appears to be a problem:
ConsoleExample started
log4net: log4net assembly [log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=692fbea5521e1304]. Loaded from [Global Assembly Cache]. (.NET Runtime [4.0.30319.34014] on Microsoft Windows NT 6.3.9600.0)
log4net: DefaultRepositorySelector: defaultRepositoryType[log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository for assembly [DelphiaLibrary, Version=1.0.0.18979, Culture=neutral, PublicKeyToken=null]
log4net: DefaultRepositorySelector: Assembly [DelphiaLibrary, Version=1.0.0.18979, Culture=neutral, PublicKeyToken=null] Loaded From [C:\Projects\DelphiaLibrary\ConsoleExample\bin\Debug\DelphiaLibrary.dll]
--> log4net: DefaultRepositorySelector: Assembly [DelphiaLibrary, Version=1.0.0.18979, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: DefaultRepositorySelector: Assembly [DelphiaLibrary, Version=1.0.0.18979, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]
Upvotes: 1
Views: 2980
Reputation: 27944
When you have multiple application logging to the same file you will need to configure a locking model. When setting the locking to MinimalLock, the applications will only lock the file when writing. This can be done by adding the lockingModel node to your appender:
<appender name="DebugFileAppender" type="log4net.Appender.RollingFileAppender" >
**<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>**
[...]
</appender>
Upvotes: 0
Reputation: 1399
I use log4net in the same way, the config is written in the application files app.config or web.config and the file watcher configured in the application code. The library only logs using the same static log param as you have used. My guess is that your dual assembly config calls are causing an issue. Try removing the one from the library.
Upvotes: 1