4

I have a simple console app. Downloaded log4net nuget package as well as log4net raygun nuget package: https://www.nuget.org/packages/log4net.Raygun/. I've set up my app to log exceptions and info messages and I do get those in my log file, but NOT in raygun dashboard, Nor do I get emails from raygun.io. What am I doing wrong?

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>

EDIT: I've added debugging and "XmlConfigurator.Configure();" this is what I get (However, still no errors show up in Raygun.io dashboard) :

...
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'
Karl Gjertsen
  • 4,690
  • 8
  • 41
  • 64
ShaneKm
  • 20,823
  • 43
  • 167
  • 296
  • 1
    You should activate the log4net internal debugging mode to check potential problems in the logging structure: https://logging.apache.org/log4net/release/faq.html (seach for "internal debugging") – samy Feb 17 '15 at 07:43
  • You can also check for misconfiguration by calling this do a test after configuration - `var messages = LogManager.GetRepository().ConfigurationMessages.Cast();` and seeing if anything is returned: another option would be to check at runtime that the raygun appender has been loaded and that it's configuration looks OK – stuartd Feb 17 '15 at 10:07

2 Answers2

4

You need to tell log4net to read the configuration, add the following line in you main method:

log4net.Config.XmlConfigurator(Watch = true);

Or put the following attribute in your assamblyinfo.cs:

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

If that is not working you can enable log4net internal debugging to see what is going on in the log4net internal code:

<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 FAQ

Peter
  • 27,590
  • 8
  • 64
  • 84
  • that fixed it. HOWEVER, I am not getting errors when running my console app in VS2013. only when I execute exe file. Why is that? – ShaneKm Feb 17 '15 at 16:03
2

A couple of suggestions. Your CurrentDomain_UnhandledException handler is passing the UnhandledExceptionEventArgs object to log4net for logging instead of the ExceptionObject on the event. For log4net.Raygun to process the exception you probably want something like this:

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

The other issue you're having is that by default log4net.Raygun will send the message to Raygun in a background task. In your simple console app, your application is exiting and unloading before the background task completes, so the message is never actually sent. You can disable the background sending of errors by adding the following setting to your appender configuration:

<appender name="RaygunAppender" type="log4net.Raygun.RaygunAppender, log4net.Raygun">
...
    <sendInBackground value="false" />
...
</appender>
plmw
  • 321
  • 1
  • 8