Just to get you started, I have created this very basic guide to get something into your log file.
Watch out for sections like this in your config:
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
It might cause some problems, so perhaps just delete it!
- Use NuGet to install log4net.
- Create a logger instance globally in your code:
private static readonly ILog logger = LogManager.GetLogger(typeof(MainWindow)); - Configure the code to look for XML configuration using XmlConfigurator:
XmlConfigurator.Configure(); - Locate you Assembly.cs file and insert the following line to let log4net know that you are using your App.config file for the configuration of log4net.
[assembly: XmlConfigurator(Watch = true)] - Insert this into your App.config file:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="example.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %thread %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="RollingFile" />
</root>
</log4net> - Start logging by calling any of the logging methods on your logger instance:
logger.Info("My first log entry");
Watch out for sections like this in your config:
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
It might cause some problems, so perhaps just delete it!
Ingen kommentarer:
Send en kommentar