달력

42024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

http://haacked.com/archive/2005/03/07/2317.aspx

 

Quick and Dirty Guide to Configuring Log4Net For Web Applications

Looking around, I noticed a lot of people struggling with getting Log4Net to work with their web applications (ASP.NET 1.1). I'm not going to spend a lot of time digging into Log4Net here, as you can do a Google search for that. But I will give you a quick and dirty guide to quickly getting it set up for a website. Bar of soap not included.

Using a Separate Config File
Although you can put your Log4Net configuration settings within the web.config file, I prefer to use a separate configuration file. Log4Net is a bit of an elitist. It won't dare put a FileSystemWatcher on web.config nor App.config. However, if you tell it to use its own config file, it will gladly monitor that log file and update its settings on the fly when the file changes.

Specifying the Log4Net Config File
If you use a separate config file, a quick and easy (and dirty) way to have your application find it is to place the config file in the webroot and add the following attribute to your AssemblyInfo.cs file.

[assembly: log4net.Config.DOMConfigurator( ConfigFile="Log4Net.config",Watch=true )]

Declaring the Logger
At the top of each class that I plan to use logging in, I declare a logger like so:

private static readonly ILog Log = LogManager.GetLogger( MethodBase.GetCurrentMethod().DeclaringType);
The reason I place a logger in each class is to scope it to that class. If you read the log4Net docs, you'll see what I mean by this.

Using the Logger
Once you've declared the logger, you can call one its logging methods. Each method is named for the logging level. For example:

Log.Debug("This is a DEBUG level message.  It's typically your most VERBOSE level."); Now whether that message shows up in your logs depends on how you've configured your appenders and the logging level you've set. Don't understand what that means? Read the Log4Net introduction.

Sample Web Solution
In order to make all this discussion very concrete, I've gone ahead and did all your homework for you by creating a simple ASP.NET 1.1 web solution (Log4NetSampleSolution.zip ) using Visual Studio.NET 2003. After unzipping this solution, you should be able to build it and then view the default.aspx web page. This page will log a few very interesting messages of varying levels to three appenders.

Of special note is the use of the RollingFileAppender as seen in this snippet.

<appender name="RollingLogFileAppender"        type="log4net.Appender.RollingFileAppender">    <file value="..\\Logs\\CurrentLog" />    <appendToFile value="true" />    <datePattern value="yyyyMMdd" />    <rollingStyle value="Date" />    <filter type="log4net.Filter.LevelRangeFilter">        <acceptOnMatch value="true" />        <levelMin value="INFO" />        <levelMax value="FATAL" />    </filter>    <layout type="log4net.Layout.PatternLayout">        <conversionPattern         value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />    </layout></appender>

Note that the file value (with backslashes escaped) points to ..\Logs\CurrentLog. This specifies that Log4Net will log to a file in a directory named "Logs" parallel to the webroot. You need to give the ASPNET user write permission to this directory, which is why it is generally a good idea to leave it out of the webroot. Not to mention the potential for an IIS misconfiguration that allows the average Joe to snoop through your logs.

[Listening to: Envy / Faith - Deep Dish - Global Underground 021 - Moscow CD2 (4:51)]

Feedback

# re: Quick and Dirty Guide to Configuring Log4Net For Web Applications

Exactly what I was trying to accomplish. Thanks.
3/12/2005 8:42 AM | sam

# re: Quick and Dirty Guide to Configuring Log4Net For Web Applications

Great info - thanks. This works for a website and the classes (Page) objects in the website. What about when the website uses different class libraries? How do I configure the class libraries so that they, too, will log to the same location as the website code does? Can I have the class libraries point to the log4net config file that is used by the website?

Thanks in advance.
3/17/2005 7:36 AM | bags

# re: Quick and Dirty Guide to Configuring Log4Net For Web Applications

That's the beauty of it. If your classes are declaring a logger, they will log to wherever the executing application is configured to log to.

If you think of your application as an assembly graph, you need to configure your assembly and all the rest pick up that setting.

Typically your root assembly is your website or your EXE.

Hope that helps.
3/17/2005 8:07 AM | Haacked

# re: Quick and Dirty Guide to Configuring Log4Net For Web Applications

Thanks a lot. You don't believe I have been looking for it since 41/2 hours, most of the examples out there: either they are difficult to configure or difficult to understand.
Good work, keep it up
4/1/2005 12:17 PM | coolguy

# re: Quick and Dirty Guide to Configuring Log4Net For Web Applications

Glad it helped! Thanks for the compliment.
4/1/2005 12:51 PM | Haacked

# re: Quick and Dirty Guide to Configuring Log4Net For Web Applications

Hello,

We are evaluating log4net for one of our customers.

Our application consists of Asp.Net application which uses remoting.
We have included log4net in both web application and remoting application.
but the log4net logs the debug info only for the web application and not in=
the remoting application.
It doesn't even creat the log file in the remoting application directory.
we have given necessary permissions for the folders.

Help on the same is much appriciated. Thanks!

Regards,
Rohit

The following is mentioned in web.config of remoting application
<configSections>
<section name=3D"log4net" type=3D"log4net.Config.Log4NetConfigurationSect=
ionHandler,log4net" />
</configSections>

<log4net>
<appender name=3D"FileAppender" type=3D"log4net.Appender.RollingFileAppen=
der">
<param name=3D"File" value=3D"Test.log" />
<param name=3D"AppendToFile" value=3D"true" />
<param name=3D"RollingStyle" value=3D"Date" />
<param name=3D"MaxSizeRollBackups" value=3D"30" />
<param name=3D"DatePattern" value=3D"yyyyMMdd" />
<layout type=3D"log4net.Layout.PatternLayout">
<param name=3D"ConversionPattern" value=3D"%d [%t] %-5p %c [%x] - %m%n"=
/>
</layout>
<filter type=3D"log4net.Filter.LevelRangeFilter">
<levelMin value=3D"DEBUG" />
<levelMax value=3D"WARN" />
</filter>
</appender>
<appender name=3D"EventLogAppender" type=3D"log4net.Appender.EventLogAppe=
nder">
<applicationName value=3D"ApplicationName" />
<layout type=3D"log4net.Layout.PatternLayout">
<conversionPattern value=3D"%date [%thread] %-5level %logger [%ndc] - %=
message%newline" />
</layout>
<filter type=3D"log4net.Filter.LevelRangeFilter">
<levelMin value=3D"ERROR" />
<levelMax value=3D"FATAL" />
</filter>
</appender>
<root>
<level value=3D"DEBUG" />
<appender-ref ref=3D"FileAppender" />
<appender-ref ref=3D"EventLogAppender" />
</root>
</log4net>

Also in the AssemblyInfo file we have added this line
[assembly: log4net.Config.DOMConfigurator(Watch=3Dtrue)]=
4/9/2005 4:26 AM | Rohit

# re: Quick and Dirty Guide to Configuring Log4Net For Web Applications

Rohit, which version of Log4Net are you using. The config sample you sent me uses a different syntax than what I used.
4/9/2005 10:15 AM | Haacked

# re: Quick and Dirty Guide to Configuring Log4Net For Web Applications

Thanks for the reply, we are using .Net framework version 1.1 and log4net version log4net-1.2.0-beta8. Please let me know where we are going wrong.
I also noticed that log.IsDebugEnabled(also any other IsxxxEnabled) function always returns false. I tried with the same configuration with other test projects i created it works but it doesn't work with my actual application. [It doesn't work with the Remoting application but same configuration works with web application]
Thanks again fro the reply...
4/12/2005 4:24 AM | Rohit

# re: Quick and Dirty Guide to Configuring Log4Net For Web Applications

I'm using version 1.2.0.30714 and the only thing I noticed is that your configuration file was very different from mine in how you declared the appenders.
4/14/2005 8:28 AM | Haacked

# re: Quick and Dirty Guide to Configuring Log4Net For Web Applications

Hi all,

I need help to set the log4net to delete log files automatically after 10days.
4/21/2005 1:24 PM | AspNet

# re: Quick and Dirty Guide to Configuring Log4Net For Web Applications

Hi

I'm simply trying to get NUnit to run some tests that log, with the Test project being the one that starts the application logging. In fact, ive seen this before and this is what im trying todo: im using a configuration class (that will utlimately read from the registry or db) that says where the configuration path should be, and that in turn tells to write to file, ill write out the TestFixture first and then the config file. I dont get any errors but neither do i get a file created or written to... thanks for any help!

[TestFixture]
public class TestLogging
{

static TestLogging()
{
string configFile = Configuration.LogConfigFilePath;
log4net.Config.DOMConfigurator.Configure(new System.IO.FileInfo(configFile));
ILog log = LogManager.GetLogger(typeof(TestLogging));
log.Info("Starting Logging");
}

[Test]
public void Test()
{
ILog logger = LogManager.GetLogger("my logger");
logger.Info("Logging the Test() method");
}
}





<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="c:\logs\log-file.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
5/17/2005 10:11 AM | Al

# re: Quick and Dirty Guide to Configuring Log4Net For Web Applications

Great, just what I needed. Been pounding my head against the wall for last couple of hours, trying to get it work, and this was excactly what I needed to get starting.

I've made 2 slight chances though

1. I put the assembly line in the global.asax instead of the assembly.cs. As far as I can tell it makes no difference and it just seems a bit more 'logical' place to me - purely a question of taste (and of where I suspect my collegues will be able to find it :)

2. I've used
[assembly: log4net.Config.XmlConfigurator( ConfigFile="Log4Net.config",Watch=true )]

Although it compiled just fine, I got a warning saying that the DOMConfigurator was obsolete and that the XmlConfigurator should be used instead - so I did :)


/emil@obey.your.compiler.or.die.com
5/20/2005 7:20 PM | Emil
Posted by tornado
|