How To Configure Log4Net Without Modifying Web.Config


Peace be upon you,

I was trying to configure Log4Net without making any modifications to the application web.config and going through the hassle of XML nodes, web.config backup specially if it was for a portal like sharepoint or something, etc.

I found a quick and easy way for such thing, I will illustrate it in steps:

1-      Add this line in your AssemblyInfo.cs file: [assembly: log4net.Config.XmlConfigurator(ConfigFile = “log4net.config”, Watch = true)]

2-      Create a file named by log4net.config and Add this example of configuration inside it:

<log4net>

<appender name=”FileAppender”>

<param name=”File” value=”C:\\tmp\\ log.txt” />

<param name=”AppendToFile” value=”true” />

<param name=”MaximumFileSize” value=”5MB” />

<layout type=”log4net.Layout.PatternLayout,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821″>

<param name=”ConversionPattern” value=”%d [%t] %-5p %c [%x] %username – %m%n%newline%newline” />

</layout>

</appender>

<appender name=”EventLogAppender” type=”log4net.Appender.EventLogAppender” >

<applicationName value=”Your Application Name” />

<layout type=”log4net.Layout.PatternLayout”>

<conversionPattern value=”%date [%thread] %-5level %logger [%property{NDC}] – %message%newline” />

</layout>

</appender>

<root name=”Logger”>

<level value=”ALL” />

<appender-ref ref=”FileAppender” />

<appender-ref ref=”EventLogAppender” />

</root>

</log4net>

3-      put it beside web.config file of your web application.

Now you are logging in both, your text file and the event log of the system, Offcourse you can make further configurations for Log4Net but that’s not our topic and that’s just a sample config file.

I find this useful for SharePointers as well, such that you can create your wsp file, deploy it using Sharepoint Solution Installer or even your own MSI installer, and just putting your log4net file in the web application virtual folder and that’s it.

Hope you find this useful 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s