Viktor Spáčil
Viktor Spáčil

Reputation: 1

Serilog not posting events to Application Insights

I use Serilog with Microsoft DI and Microsoft Logging Extension, see nugets below:

<PackageReference Include="Serilog.Extensions.Logging" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="Splat.Microsoft.Extensions.DependencyInjection" Version="15.2.22" />
<PackageReference Include="Splat.Microsoft.Extensions.Logging" Version="15.2.22" />

Code is running on macOS 15.2, .NET SDK v9.0.100 is used. This is the logger configuration:

        var logConfig = new LoggerConfiguration()
            .WriteTo.File(
                new JsonFormatter(),
                PathToLogFile,
                shared: true);
        var appInsightConnectionString = configuration.GetSection(AppInsightLoggingSection).Value;
        logConfig.WriteTo.ApplicationInsights(
                new TelemetryConfiguration { ConnectionString = appInsightConnectionString },
                TelemetryConverter.Traces,
                LogEventLevel.Fatal);
        logConfig.WriteTo.Console();

        Log.Logger = logConfig.CreateLogger();
        services.AddLogging(builder => builder.ClearProviders().AddSerilog());

Usage is as follows:

    public static void Main(string[] args)
    {
        try
        {
            BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
        }
        catch (Exception ex)
        {
            Log.Fatal(ex, "UnexpectedException");
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }

I can see the error logged in the console, in the file but Application Insight is empty. It is also interesting that when I do this:

    public static void Main(string[] args)
    {
        try
        {
            BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
        }
        catch (Exception ex)
        {
            var logger = Services.GetRequiredService<ILogger<App>>();
            logger.LogCritical("TEST");
            Log.Fatal(ex, "UnexpectedException");
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }

Then the message "TEST" is not logged to the Application Insight but the following call of Log.Fatal goes through and exception appeared logged in the Application Insight. Any ideas? Thank you.

Upvotes: 0

Views: 61

Answers (1)

Viktor Sp&#225;čil
Viktor Sp&#225;čil

Reputation: 1

I solved it! The problem was this Log.Fatal(ex, "UnexpectedException"), when I change it to this Log.Fatal(ex.Message), then the message was logged to application insight.

Upvotes: 0

Related Questions