Search code examples
asp.netnlogsentry

Sentry/NLog Integration Not Logging to Sentry


I am using the latest Sentry/NLog packages (as shown below) but for some reason, my errors are begin logged to NLog but not to Sentry.

I can see that Sentry is being initialized, but it never logs anything! Meanwhile, the errors are being logged to NLog without issue.

These are my relevant packages:

  <package id="Newtonsoft.Json" version="12.0.3" targetFramework="net472" />
  <package id="NLog" version="4.7.0" targetFramework="net472" />
  <package id="Sentry" version="2.1.1" targetFramework="net472" />
  <package id="Sentry.NLog" version="2.1.1" targetFramework="net472" />
  <package id="Sentry.PlatformAbstractions" version="1.1.0" targetFramework="net472" />
  <package id="Sentry.Protocol" version="2.1.1" targetFramework="net472" />
  <package id="System.Collections.Immutable" version="1.5.0" targetFramework="net472" />

This is my NLog config file (I've removed my DSN for security reasons):

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      throwConfigExceptions="true">
  <extensions>
    <add assembly="Sentry.NLog" />
  </extensions>
  <targets async="true">
    <target name="SentryLogger" xsi:type="Sentry"
      dsn="{removed for security reasons...}"
      environment="${appsetting:item=Environment:default=Staging}"
      InitializeSdk="true"
      layout="${message}"
      breadcrumbLayout="${message}"
      minimumBreadcrumbLevel="Debug"
      minimumEventLevel="Error">
      <options
            attachStacktrace="true"
            sendDefaultPii="true"
            shutdownTimeoutSeconds="5"
            includeEventDataOnBreadcrumbs="true" />
    </target>
    
    <target name="default" xsi:type="File"
      fileName="${basedir}/logs/${shortdate}.log"
      archiveFileName="${basedir}/logs/archive-log/app-log.{#}.txt"
      archiveEvery="Day"
      archiveNumbering="Rolling"
      maxArchiveFiles="30" />
  </targets>
  <rules>
    <logger name="*" writeTo="default" />
    <logger name="*" minlevel="Error" writeTo="SentryLogger" />
  </rules>
</nlog>

Is there any reason why NLog would be working but not Sentry? I have other projects in the same solution which correctly log to both Sentry and NLog, just not this one!

enter image description here

Edit: I checked the NLog Internal log and it seems like it's writing to Sentry, but it never shows up in the UI on Sentry. There don't appear to be any errors in the internal log file, either.

2020-06-11 16:53:31.5829 Trace AsyncWrapper(Name=default): Throttled timer scheduled
2020-06-11 16:53:31.5829 Trace AsyncWrapper(Name=SentryLogger): Throttled timer scheduled
2020-06-11 16:53:31.5920 Trace AsyncWrapper(Name=SentryLogger): Writing 1 events (Timer)
2020-06-11 16:53:31.5920 Trace AsyncWrapper(Name=default): Writing 1 events (Timer)

Edit: After updating the Sentry.NLog package via Nuget from 2.1.1 to 2.1.4, I am receiving a System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel. error in the NLog internal log file! I was able to resolve the issue by adding System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; to my Global.asax.cs file! Problem solved! Thank you, @Rolf Kristensen!

2020-06-25 16:50:55.8958 Debug Event queued up.
2020-06-25 16:50:55.9198 Debug Configuring the scope.
2020-06-25 16:50:56.2788 Debug Event 1338f238cd484cc7a0a288133306718c in-flight to Sentry. #1 in queue.
2020-06-25 16:50:56.4968 Error Error while processing event 1338f238cd484cc7a0a288133306718c: System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.
   at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
   at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Sentry.Internal.Http.GzipBufferedRequestBodyHandler.<SendAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Sentry.Internal.Http.RetryAfterHandler.<SendAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Sentry.Internal.Http.HttpTransport.<CaptureEventAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at Sentry.Internal.BackgroundWorker.<WorkerAsync>d__18.MoveNext(). #1 in queue.

Solution

  • After updating the Sentry.NLog package via Nuget from 2.1.1 to 2.1.4, I was still receiving a System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel. error in the NLog internal log file!

    However, I was able to resolve this issue by adding this to my Global.asax.cs file!

    System.Net.ServicePointManager.SecurityProtocol = 
        SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;  
    

    Problem solved! Thank you, @Rolf Kristensen!