x First time here? Check out the FAQ

Log4net for package developers

    See also: ismailmayat.wordpress.com/.../

    • Download log4net
    • In your visual studio project add a reference to log4net.dll (use bin\net\2.0\release in the distribution)
    • Open AssemblyInfo.cs in the properties folder of your solution.
    • Add the following attributes:
    [assembly: XmlConfigurator(ConfigFile = @"config\my.log4net.config", Watch = true)]
    [assembly: RepositoryAttribute("MyAppName")]

    Note: RespositoryAttribute is used to identify a distinct configuration for your unique package/assembly it can be any value you wish but try to avoid conflicts.

    By specifiying watch=true in the XmlConfigurator attribute you can modify the logging config during development and it will be automatically reloaded without causing an application restart.

    A simple configuration file could be as follows:

    <?xml version="1.0" encoding="utf-8" ?>
    <log4net>

      <appender name="R" type="log4net.Appender.RollingFileAppender">
        <file value="${TEMP}/myPackage.log" />
        <appendToFile value="true" />
        <maximumFileSize value="1000KB" />
        <maxSizeRollBackups value="2" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level %thread %logger - %message%newline" />
        </layout>
      </appender>

      <root>
        <level value="DEBUG" />
        <appender-ref ref="R" />
      </root>

    </log4net>

    The above logs to a file in your system tmp directory - The file is rotated once it reaches 1 MB and the last two versions are maintained. Log4net can log to XML files, databases, email etc and you can define different loggers for different classes and different log targets for different levels of logging. See log4net Manual - Configuration for more details.

    In your package classes add a logger declaration, for example:

    using System;
    using umbraco.BusinessLogic;
    using log4net;
    using System.Reflection;

    namespace Umb.Log4Net
    {
        public class Class1 : ApplicationBase
        {
            private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

            public Class1()
            {
                log.Debug("application starting up");
            }
        }
    }

    Note: When releasing Umbraco packages you may want to set your logging levels to NONE or FATAL to stop DEBUG information being logged.