1) Add log4net package from Manage NuGet Manager option on references right click menu.
2) Add this code in AssemblyInfo.cs file under properties in solution window.
// log4net to know and look for configuration [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
3) Add below config under "configuration" in web.config file.
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections>
4) Add this code under "configuration" in web.config file.
here file element is used to configure the application log file location.<log4net debug="true"> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="D:\\TestProj\\TestLog.txt" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="10MB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" /> </layout> </appender> <root> <level value="DEBUG" /> <appender-ref ref="RollingLogFileAppender" /> </root> </log4net>
5) Add this code under "configuration/appSettings" in web.config file.
<add key="log4net.Internal.Debug" value="true" />
6) Add this code after system.web section under "configuration" in web.config.
here initializeData attribute is used in listeners to capture log4net data.<system.diagnostics> <trace autoflush="true"> <listeners> <add name="textWriterTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="D:\\TestProjlog4net.txt" /> </listeners> </trace> </system.diagnostics>
7) Create log object using LogManager.GetLogger method under log4net namespace in class where need log operations.
using log4net; private static readonly ILog log = LogManager.GetLogger(typeof(CustomerController).Name);
8) log object has info, Debug, Error methods to log the details for application.
log.Info("Employee information adding event: " + "ID: " + employee.Id.ToString() + ", Name: " + employee.Name);
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page