Step 1. Add the following in the AssemblyInfo.cs
[assembly: log4net.Config.XmlConfiguratorAttribute(
ConfigFile = "log4net.xml", Watch = true)]
Step 2. Create the log4net.xml file and add the following to the new fileWith respect to the location of this file and SharePoint, this file can exist in the root of the wss root, next to the relevant web.config file for the given site. In general, this should sit next to the app.config or web.config file for the given application.
<log4net>
<appender name="GeneralLog" type="log4net.Appender.RollingFileAppender">
<file value="${TEMP}\\Logs\\AppName_${COMPUTERNAME} " />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<staticLogFileName value="false" />
<datePattern value=".yyyyMMdd.'log'" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="5MB" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="GeneralLog" />
<appender-ref ref="DebugAppender" />
</root>
<!-- Print only messages of level ERROR or above in the package NHibernate -->
<logger name="NHibernate" additivity="true">
<level value="ERROR" />
</logger>
</log4net>
Step 3. Add the following at the top of your classprivate static readonly log4net.ILog log = log4net.LogManager.GetLogger(
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
Step 4. Add the following line in the code where you want logging to be done
log.Info("Program Started " + DateTime.Now.ToString ());
ReferencesApache log4net Home Page - http://logging.apache.org/log4net/

4 comments:
Simple! Clear! Working! This is what I am looking for. Good job!
step 4 is better implemented as
if (log.IsInfoEnabled) log.Info("Program Started " + DateTime.Now.ToString ());
to reduce the impact of the string formatting where the log isn't enabled
Re last comment: If you want to make your (not) logging code as fast as possible, see "What is REALLY the FASTEST way of (not) logging?" in the FAQ section of documentation. Anyway, thanks for the post BigJim.
Just as a quick follow-up to this, if you're like me, and prefer to use a separate log4net configuration file, rather than storing the configuration info inside the app.config or web.config file, AND if you followed my example to a tee, you created a file named "log4net.xml".
For the record, I would advise changing that to "log4net.config", especially if you are running this on a web server, as the typical IIS/ASP.NET configuration is to NOT serve up ".config" files. That will plug a potential security hole.
Post a Comment