簡體   English   中英

Raygun與log4net沒有記錄

[英]Raygun with log4net not logging

我有一個簡單的控制台應用程序 下載了log4net nuget包以及log4net raygun nuget包: https ://www.nuget.org/packages/log4net.Raygun/。 我已經設置了我的應用程序來記錄異常和信息消息,我確實在我的日志文件中得到了這些,但不是在raygun儀表板中,我也沒有收到來自raygun.io的電子郵件。 我究竟做錯了什么?

private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    Log.Error(e);
}

private static void Main(string[] args)
{
    XmlConfigurator.Configure();

    // Unhandled exceptions
    AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
throw new Exception("test exception");

}

Web.Config中:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- log4net -->
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

  <log4net>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="ErrorLog//ErrorLog.txt" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="2" />
      <maximumFileSize value="1MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
      </layout>
    </appender>

    <appender name="RaygunAppender" type="log4net.Raygun.RaygunAppender, log4net.Raygun">
      <threshold value="DEBUG" />
      <apiKey value="MY SUPER AWESOME KEY HERE" />
      <!-- Attempt to send errors to raygun 15 times -->
      <retries value="15" />
      <!-- Wait 1 minute between retry attempts -->
      <timeBetweenRetries value="00:01:00" />
      <!-- Toggles whether to only send exceptions to raygun, or to also send messages logged to ERROR -->
      <onlySendExceptions value="false" />
      <!-- Optional filters for filtering exceptions and messages before sending to raygun -->
      <!--<exceptionFilter value="SomeOtherAssembly.SensitiveInformationMessageFilter, SomeOtherAssembly" />
      <renderedMessageFilter value="SomeOtherAssembly.SensitiveInformationMessageFilter, SomeOtherAssembly" />-->
    </appender>

    <root>
      <level value="ALL" />
      <appender-ref ref="LogFileAppender" />
      <appender-ref ref="RaygunAppender" />
    </root>
  </log4net>

    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.2.13.0" newVersion="1.2.13.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

編輯:我添加了調試和“XmlConfigurator.Configure();” 這是我得到的(但是,Raygun.io儀表板中仍然沒有出現錯誤):

...
log4net: RaygunAppender: Building Raygun message
log4net: RaygunAppender: Resolving application assembly
log4net: RaygunAppender: No exception object found in error, creating raygun error message from the rendered message and calling class
log4net: RaygunAppender: Sending Raygun message in a background task. Retries: '15', TimeBetweenRetries: '00:01:00'

您需要告訴log4net讀取配置,在main方法中添加以下行:

log4net.Config.XmlConfigurator(Watch = true);

或者在assamblyinfo.cs中添加以下屬性:

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

如果這不起作用,您可以啟用log4net內部調試以查看log4net內部代碼中發生的情況:

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

...

    <system.diagnostics>
        <trace autoflush="true">
            <listeners>
                <add 
                    name="textWriterTraceListener" 
                    type="System.Diagnostics.TextWriterTraceListener" 
                    initializeData="C:\tmp\log4net.txt" />
            </listeners>
        </trace>
    </system.diagnostics>
</configuration>

Log4net常見問題

一些建議。 您的CurrentDomain_UnhandledException處理程序將UnhandledExceptionEventArgs對象傳遞給log4net以進行日志記錄,而不是事件上的ExceptionObject 對於log4net.Raygun來處理異常,你可能想要這樣的東西:

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    Log.Error(e.ExceptionObject);
}

您遇到的另一個問題是默認情況下log4net.Raygun會在后台任務中將消息發送給Raygun。 在您的簡單控制台應用程序中,您的應用程序在后台任務完成之前退出和卸載,因此消息實際上從未發送過。 您可以通過將以下設置添加到appender配置來禁用后台發送錯誤:

<appender name="RaygunAppender" type="log4net.Raygun.RaygunAppender, log4net.Raygun">
...
    <sendInBackground value="false" />
...
</appender>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM