Reputation: 3422
I am trying to use log4net by following a few tutorials and reading the plethora of posts about people having problems with it, since I can not make it work properly.
I have this part right on top of my .config
file
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="log4net.log" />
<appendToFile value="true" />
<maximumFileSize value="500KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="All" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
I added this line on my AssemblyInfo file
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
And tried using the logger inside mt service like this
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
or
private static ILog Log = LogManager.GetLogger(typeof(MyService));
No file is ever created though, nor any other indication that something is happening at all.
How can I properly use this logger?
Upvotes: 2
Views: 1772
Reputation: 38335
Create a log4net DebugAppender to view the output of log4net in the Visual Studio output window which should give you some indications to the issue.
Add to the app.config:
<appSettings>
<!-- log4net configuration when running in debug mode. -->
<add key="log4net.Internal.Debug" value="true" />
</appSettings>
Add a new debug appender to the log4net.config:
<appender name="DebugAppender" type="log4net.Appender.DebugAppender">
<immediateFlush value="true" />
<layout type="log4net.Layout.SimpleLayout" />
</appender>
And add the new appender to the log4net.config root:
<root>
<level value="ALL" />
<appender-ref ref="RollingFile" />
<appender-ref ref="DebugAppender" />
</root>
Run the application and view the Visual Studio output window to view the internal logging for log4net. If there is no output, then the log4net.config file is never loading.
Upvotes: 5