Recommendations for creating a log library using log4net. Is an

My goal is to create a log4net library available for several projects.

In my solution, which is in .net 4.0, I created the Logger class library and referenced it from a web project.

Now I created logger.config in the class library and put the entire configuration in the logger.config file. Then i used

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

When I launch the web application, nothing is logged. So I added this line of code in web.config

<add key="log4net.Internal.Debug" value="true"/> 

which gave me info and debug info

Could not find configuration section "log4net" in application .config file. Check the .config file for elements and. The configuration section should look like this:

I moved the configuration from logger.config to web.config and everything works fine.

log4net web.config, logger.config . , , .

: ? ? .

FYI, IOC .

+5
3

, , , , :

http://slf.codeplex.com/

http://netcommon.sourceforge.net/

.Net.

, ( ), :

, , - :

interface ILogger
{
    void Debug(string message);
    void Info(string message);
    void Warn(string message);
    …..
}

, , :

x.For<ILogger>().Singleton().Use<MyLogger>();

, , , , , , / .

, , , , XmlConfigurator , , ,

<!-- Relative to the application root path -->
<add key="log4net.Config" value="Logger.log4net"/>
<add key="log4net.Config.Watch" value="True"/>

. log4net, . :

<?xml version="1.0" encoding="utf-8" ?>

<!-- This section contains the log4net configuration settings -->
<log4net>

  <!-- Define some output appenders -->

  <appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
    <file value="webapp-log.txt" />
    <appendToFile value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
    </layout>
  </appender>

  <!-- Setup the root category, add the appenders and set the default priority -->

  <root>
    <level value="DEBUG" />
    <appender-ref ref="LogFileAppender" />
  </root>

</log4net>

:

, - ( Windows ) log4net

, Nuget- ( IIS) , , . , Nuget, log4net, Nuget -

+9

, - application_start (gloabal.asax.cs)

    void Application_Start(object sender, EventArgs e)
    {
        log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("Logger.config")));
    }
+3

: http://forums.asp.net/t/1739162.aspx/1 . - webconfig configs /, , . , , appConfig log4net:

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

  <log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="logfile.txt"/>
      <appendToFile value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline"/>
      </layout>
    </appender>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="FileAppender"/>
    </root>
  </log4net>

ps also check this answer: Can the web.config file be read from an external XML file? Make sure you also unmount the configuration file if the web.config file does not include it.

+2
source

All Articles