Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include log4net for a class library?

I want to implement logging function into a class library, which is itself referenced in a webservice. I tried to add app.config and did everything needed, but it seems that when an exception is thrown, log4net simply does nothing.

my app.config

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="D:\\mylogfile.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <filter type="log4net.Filter.StringMatchFilter">
        <stringToMatch value="test" />
      </filter>
      <filter type="log4net.Filter.StringMatchFilter">
        <stringToMatch value="error" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline%exception" />
      </layout>
    </appender>
    <root>
      <level value="INFO"/>
      <appender-ref ref="RollingFileAppender"/>
      <appender-ref ref="ConsoleAppender" />
    </root>
  </log4net>

in AssemblyInfo.cs:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "app.config")]

in LogManager.cs:

private static readonly ILog Log = log4net.LogManager.GetLogger
            (MethodBase.GetCurrentMethod().DeclaringType);
public static void WriteLog(Exception ex)
{
    Log.Error(ex);
}

Can you please tell me what's wrong? How can I get log4net working for my class library?

Thank you

like image 250
Quan Mai Avatar asked Jan 06 '11 07:01

Quan Mai


People also ask

Where do I put log4net in web config?

Now, add the section "<log4net></log4net>" after the <configSections/> element in your app. config file. Next, inside the "<log4net></log4net>" section, place the configuration details as shown in the code snippet given below. That's all you need to do to configure log4net.

How do I add a class library to my project?

Right-click on the solution in Solution Explorer and select Add > New Project. On the Add a new project page, enter library in the search box. Choose C# or Visual Basic from the Language list, and then choose All platforms from the Platform list. Choose the Class Library template, and then choose Next.


2 Answers

At runtime, config file is always used from host application, unless declared explicitly. Here in case, web.config is being used not app.cofig. Rather mention some other custom config file name and ensure that file is copied in virtual directory of web service. Also as chibacity said, ensure permission on log file. Better keep it in app_data folder for web service host.

like image 160
hungryMind Avatar answered Oct 03 '22 06:10

hungryMind


You could use some kind of injection, either you build a simple one like:

public interface ILogger
{
    void LogInfo(string message);
       .
       .
       .
}

And then you just inject something that matches that interface, like a wrapper for log4net or such.

But, I think the most correct thing is to not log in the class library. Why I think so is because the library itself is not the application, your web service is so your web service should decide what to log. If you want to log exceptions just don't catch them in the class library and let your web service handle the logging. This will also make it easier to centralize the logging.

like image 40
Tomas Jansson Avatar answered Oct 03 '22 06:10

Tomas Jansson