Log4net not loggin dlls errors or website errors

时间:2015-11-12 11:38:20

标签: c# asp.net dll webforms log4net

Ok I am usng log4net to log exceptions in a dll but nothing is being ouputed

I added the following

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

To AssemblyInfo which I persume is all i neded to do and then initiate the logger on my class as follows

 private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(SocerDataAccess));

But OBV the dll does not no nothing of the structure of the website so how do i configure the dlll to use the same debug file as website with the web.config I have the following.

<log4net>
<appender name="RollingFileAppenderAll" type="log4net.Appender.RollingFileAppender">
  <file value="debug.txt" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <datePattern value=".yyyyMMdd" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="1MB" />
  <staticLogFileName value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.Pattern Layout">
    <conversionPattern value="%date [%thread] %-6level %logger - %message %exception%newline" />
  </layout>
  <filter type="log4net.Filter.LoggerMatchFilter">
    <loggerToMatch value="Jayway.ProjectX" />
  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />
</appender>
<root>
  <level value="ALL" />
  <appender-ref ref="RollingFileAppenderAll" />
</root>

My question is what do i need to do to get this to work in this scneraio i put in my Global.asx the following

    protected void Application_Error(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("Unhandled error occured in application. Sender: ");
        sb.AppendLine(Request.RawUrl);
        sb.Append("Query: ");
        sb.AppendLine(Request.QueryString.ToString());

        Exception ex = Server.GetLastError().GetBaseException();

        Log.Debug(ex.ToString());

        Server.ClearError();
    }

and in my application start to test that the logger is working

  protected void Application_Start(object sender, EventArgs e)
    {
        Log.Info("Start Of Unite CMS");
    }

But nothing is getting outputed does anybody no why.

1 个答案:

答案 0 :(得分:1)

Configure log4net at the Application_Start() method like this

  void Application_Start(object sender, EventArgs e) {
          // Code that runs on application startup
          log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config")));
     }

for more detail here example