Customize log4net Output Path

The default log4net output path is relative to the startup directory of your executable. To customize the output path, you can override the base Appender’s File property in your own Appender class. The following example describes how to route the log files relative to the temp folder : public class MyOwnAppender : log4net.Appender.RollingFileAppender { public override string File { get { return base.File; } set { if (Path.IsPathRooted(value)) { base.File = value; } else { base.File = Path.Combine(Path.GetTempPath(), value); } } } } Your log4net config file must be modified so that log4net can locate and use your custom Appender. If MyOwnAppender is in MyNamespace and you have implemented it in MyLoggerLib.dll :

Leveraging XPATH Axes

XPATH axes come in very handy when nodes need to be selected based on (attribute’s or child element’s) values that must match those of other nodes elsewhere in the xml tree. Consider the following log4net configuration file : <?xml version=”1.0″?> <configuration> <configSections> <section name=”nhibernate” type=”System.Configuration.NameValueSectionHandler, System, Version=,Culture=neutral, PublicKeyToken=b77a5c561934e089″ /> <section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler,log4net” /> </configSections> <nhibernate> <add key=”hibernate.connection.provider” value=”NHibernate.Connection.DriverConnectionProvider” /> <add key=”hibernate.connection.driver_class” value=”NHibernate.Driver.SqlClientDriver” /> <add key=”hibernate.connection.connection_string” value=”Server=servername; Database=dbname; User=username; Password=secret;” /> <add key=”hibernate.connection.isolation” value=”ReadCommitted” /> <add key=”hibernate.dialect” value=”NHibernate.Dialect.MsSql2000Dialect” /> </nhibernate> <log4net> <appender name=”NHibernateFileLog” type=”log4net.Appender.RollingFileAppender”> <file value=”Logs/nhibernate.txt” /> <appendToFile value=”true” /> <rollingStyle value=”Size” /> <maxSizeRollBackups value=”10″ /> <maximumFileSize value=”100KB” /> <staticLogFileName value=”true” /> <layout type=”log4net.Layout.PatternLayout”> <conversionPattern value=”%d{HH:mm:ss.fff} [%t] %-5p %c – %m%n” /> </layout> </appender> <appender name=”GeneralLog” type=”log4net.Appender.RollingFileAppender”> <file value=”Logs/general.txt” /> <appendToFile […]