1.
Right click on Solution and select ‘Manage NuGet
Packages for Solution…’.
2.
Click on Browse, search ‘log4net’ and install
Log4net.
3.
Add [assembly: log4net.Config.XmlConfigurator(Watch = true)] after [assembly: AssemblyCulture("")] line in ‘AssemblyInfo.cs’
under Properties.
4.
Add private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
to the class where you want to add logging.
5.
Add below line to App.config under “Configuration”
tag.
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,
log4net" />
</configSections>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="MyAppender" />
<appender-ref ref="MyFileAppender" />
</root>
<appender name="MyAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger -
%message%newline" />
</layout>
</appender>
<appender name="MyFileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="C:\\TestLog.log" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger -
%message%newline" />
</layout>
</appender>
</log4net>
<startup>
Enjoy logging……….
No comments:
Post a Comment