Using Log4Net

curiouswebster used Ask the Experts™
Any advice on using Log4Net as my exception handler/logger in my C# WinForms program?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2008
It is a great choice, and pretty easy to use.

Quick start:

1) Add a Log4Net config file to your application.

<?xml version="1.0" encoding="utf-8" ?>
    <appender name="FileAppender" type="log4net.Appender.FileAppender" >
      <file value="c:\temp\log-file.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="500KB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{dd MMM yyyy HH:mm:ss}: %m%n" />
      <level value="ALL" />
      <appender-ref ref="FileAppender" />

2) Modify AssemblyInfo.cs to add attribute:

     [assembly: XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

2) Define a logger:

        private static log4net.ILog _eventLog = null;

        public static log4net.ILog EventLog
            get { return _eventLog; }

3) Create an instance of the logger:

    _eventLog = log4net.LogManager.GetLogger("logger name");

4) Write something to the log:

     _eventLog.Info("Starting socket service");
curiouswebsterSoftware Engineer



Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial