Search code examples
asp.netazureazure-devopsazure-web-app-servicesentry

Web Application not logging errors to Sentry when deployed on Azure App


I am using Sentry free account for logging error in my application. The logging works fine on my dev environment but when I deploy app on Azure, errors are not pushed on sentry. I checked file logs and the Sentry client CaptureException method is working fine and returning a SentryId as well but when I check at Sentry.io, no exception is logged there. It is an asp. net application. .Net Framework: 4. 6. 1 Sentry Client Version: 3. 9. 0. 0

This is my method that logs exceptions to sentry:

public void LogException(Exception ex)
        {
            
                try
                {
                    if (ex == null) return;
                    var sentryId = SentrySdk.CaptureException(ex);
                    LogManager.Info(typeof(SentryManager),
                    $"SentrySdk.CaptureException(ex) called successfully. SentryId: {sentryId}");
                }
                catch (Exception e)
                {
                    LogManager.Error(typeof(SentryManager), $"SentryManager Exception - {e.Message + e.InnerException}", null);
                }
        }

I have checked my Azure logs and nothing found related to Sentry. Please suggest what can I do to debug my issue, Thanks.

--EDIT--

I am able to get the error message. I am getting this error: "Failed to report an error on a session because there is none active.System.Object[]"

Can anyone suggest what to do to avoid this?

--EDIT 2--

Error Captured: Could not load file or assembly ‘System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1,


Solution

  • At first, I had to identify the error that was causing the issue. For this, I wrote a simple logger for logging messages generated from Sentry SDK. (Source: Sentry Not Logging Errors in WebForms App)

    public class SentryDiagnosticLogger : IDiagnosticLogger {
        public SentryDiagnosticLogger()
        {
        }
    
        public bool IsEnabled(SentryLevel level)
        {
            return true;
        }
    
        public void Log(SentryLevel logLevel, string message, Exception exception = null, params object[] args)
        {
            if (exception != null)
                throw new Exception("Sentry Exception occurred", exception);
        }
    }
    

    My SDK initialization code looks like this:

    SentrySdk.Init(o =>
        {
            o.Dsn = config;
            o.Environment = ConfigurationManager.AppSettings["tavrtalk:Environment"];
            o.Debug = true;
            o.DiagnosticLevel = SentryLevel.Debug;
            o.DiagnosticLogger = new SentryDiagnosticLogger();
        });
    

    Finally, I got the error:

    Could not load file or assembly System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1

    I had 5.0.0 version in my project. So, I updated binding redirects in my host application and Sentry SDK automatically picked the update version of System.Runtime.CompilerServices.Unsafe assembly. I added the following configurations in my web.config file:

    <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="*************" culture="neutral" />
          <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
    </dependentAssembly>
    

    This was the reason why the Sentry was working fine on dev because web.config for my dev had this binding redirect but the deployed web.config didn't.