Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
breakoutModefull-width
languagec#
public class AzureInsightsMiddleware(
    RequestDelegate next,
    TelemetryClient telemetryClient,
    IConfiguration configuration
)
{
    public async Task Invoke(HttpContext context)
    {
        bool applicationInsightsEnabled = configuration.GetValue <bool>GetValue<bool> (
            "ApplicationInsights:TelemetryFeatureEnabled"
        );

        try
        {
            await next(context);
        }
        catch (Exception ex)
        {
            int exceptionLoggingOption = configuration.GetValue <int> GetValue<int>("Exception:LoggingOption");

            if (applicationInsightsEnabled && exceptionLoggingOption != -1)
            {
                PathString requestPath = context.Request.Path;
                string requestMethod = context.Request.Method;

                List < LogRequest >List<LogRequest> logRequests = configuration.GetSection("Exception:LogRequests").Get <List<LogRequest>> Get<List<LogRequest>>() ?? [];

                bool shouldLog = (logRequests.Any(
                    logRequest =>
                    requestMethod.Equals(
                        logRequest.Method,
                        StringComparison.OrdinalIgnoreCase
                    ) && requestPath.StartsWithSegments(logRequest.Path)
                ) && exceptionLoggingOption == 0) || exceptionLoggingOption == 1;

                if (shouldLog)
                {
                    var customMetaData = new Dictionary <stringDictionary<string, string>()
                        {
                            {
                                "exception_message",
                                ex.Message
                            },
                            {
                                "exception_stack_trace",
                                ex.StackTrace
                            },
                            {
                                "exception_inner_message",
                                ex?.InnerException?.Message ?? string.Empty
                            },
                            {
                                "exception_inner_stack_trace",
                                ex?.InnerException?.StackTrace ?? string.Empty
                            },
                            {
                                "exception",
                                ex.ToString()
                            },
                            {
                                "inner_exception",
                                ex?.InnerException?.ToString() ?? string.Empty
                            },
                        };

                    if (context.User.Claims.Any())
                    {
                        string userDatabase = context.User.Claims.FirstOrDefault(c => c.Type == "Database")?.Value ?? string.Empty;
                        customMetaData.Add("organisation", userDatabase);
                    }
                    else
                    {
                        customMetaData.Add("organisation", "not-specified");
                    }

                    telemetryClient.TrackEvent($ "Response Exception", customMetaData);
                }
            }
        }
    }
}

...