Web.config configuration when using separate Project for logging Log4Net

StackOverflow https://stackoverflow.com/questions/23588204

  •  19-07-2023
  •  | 
  •  

سؤال

I have a Visual Studio solution with 2 proyects:

  1. Web Application
  2. Library project for logging

My Web Application project doesn't have Log4Net reference. The Logging Project is the one that has the reference to the Log4Net dll and it has a Manager Class where I have all the methods for logging.

Now, I want to know what do I have to do in the Web Application WEb.Config file, I saw on the internet that I have to add a Section:

 <section name="log4net" 
      type="log4net.Config.Log4NetConfigurationSectionHandler, 
            log4net"/>

But my concern is that my web project doesn't have the Log4Net dll reference. Is there I way I can do that without adding Log4Net dll to my web project?

هل كانت مفيدة؟

المحلول

Yes, you can add the section element for log4net without referencing log4net.dll in your web application project.

To be sure, I double checked this with a scratch web application project (WebApplication1) and class library (ClassLibrary1) targeting .NET 4.0 in Visual Studio 2013. ClassLibrary1 referenced log4net.dll, but WebApplication1 did not; it just referenced ClassLibrary1 and had a log4net section element & related log4net element in its Web.config: logging to a text file via log4net worked beautifully.

نصائح أخرى

It seems you have forgotten to configure log4net. Best practice is to add a configure attribute in your assably.cs:

// Configure log4net using the .config file
[assembly: log4net.Config.XmlConfigurator(Watch=true)]
// This will cause log4net to look for a configuration file
// called TestApp.exe.config in the application base
// directory (i.e. the directory containing TestApp.exe)
// The config file will be watched for changes.

// Configure log4net using the .log4net file
[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension="log4net",Watch=true)]
// This will cause log4net to look for a configuration file
// called TestApp.exe.log4net in the application base
// directory (i.e. the directory containing TestApp.ex

You can add the attribute in your log4net dll, there is no need for a reference to log4net in you web project.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top