skip to Main Content

I am trying to log messages to a Graylog instance. I have setup the code to do so in a Asp.net core application and everything works fine. However, I am getting issues trying to do so from a console app. The Graylog is running in Docker Desktop on my windows machine. in my Graylog instance, I have both a UDP input and an Http input active.

For the ASP.Net Core application, this is the Setup in program.cs

        builder.Host.UseSerilog((context, configuration) =>
        {
            configuration
                  .ReadFrom.Configuration(context.Configuration)
                  .WriteTo.Graylog(new GraylogSinkOptions()
                  {
                      HostnameOrAddress = "localhost",
                      Port = 12201,
                      TransportType = TransportType.Http
                  });
        });

And then a little further down in my program.cs file.

        app.UseSerilogRequestLogging();

When I start the ASP.Net app, I get my log messages in Graylog. These are the relevant packages.

    <PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
    <PackageReference Include="Serilog.Sinks.Graylog" Version="3.1.1" />

Flipping over to the Console App, this is the setup.

            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.Console()
                .WriteTo.File("log.txt",
                  rollingInterval: RollingInterval.Day,
                   rollOnFileSizeLimit: true)
                .WriteTo.Graylog(new GraylogSinkOptions()
                {
                    HostnameOrAddress = "localhost",
                    Port = 12201,
                    TransportType = TransportType.Http,
                    Facility = "Test App"

                })
                .CreateLogger();

When logging anything from here, it logs to the Console window, and it logs to the file just fine. However, nothing is going to Graylog. I used Fiddler classic and was able to see all the traffic coming from the web app, AND I was able to see other calls from my console app that were accessing Graylog APIs using HttpClient, but no traffic for the logging itself.

These are the packages I installed for the console.

    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
    <PackageReference Include="Serilog" Version="4.0.0" />
    <PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
    <PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
    <PackageReference Include="Serilog.Sinks.Graylog" Version="3.1.1" />

I also added this line to the setup to try to help with debugging.

Serilog.Debugging.SelfLog.Enable(msg => Console.WriteLine(msg));

This never is activated so from what I can tell, either nothing is happening or Serilog is oblivious to the sink not working. I have searched SO, ChatGPT and general search engines. This should be working but it is not.

Edit: I tried version 3.0.2 of the Serilog.Graylog.Sink library in my console app and that works. Any version higher than that does not work in the Console app but does work in the ASP.Net Core app. Something appears to not be compatible.

Edit 2: One commenter asked for a minimal reproducible example. While all of the code in here is pretty much it, I have consolidate the program down to the contents of one main function which is misbehaving on my setup. The code runs with no exceptions or evidence that something went wrong, it just doesn’t show up in the logs like the messages do from my ASP.Net Core app which is setup very similar.

        static async Task Main(string[] args)
    {
        Serilog.Debugging.SelfLog.Enable(msg => Console.WriteLine(msg));

        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .Enrich.FromLogContext()
            .WriteTo.Graylog(new GraylogSinkOptions()
            {
                HostnameOrAddress = "localhost",
                Port = 12201,
                TransportType = TransportType.Http,
                Facility = "Test App",
            })
            .CreateLogger();
        Log.Information("This is a test message");
        Log.CloseAndFlush();

    }

2

Answers


  1. .MinimumLevel.Debug()
    

    Try to switch it first to

    .MinimumLevel.Information()
    

    see what will happen

    Login or Signup to reply.
  2. Try switching to TransportType.Udp

    Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.Console()
                .WriteTo.Graylog(new GraylogSinkOptions()
                {
                    HostnameOrAddress = "localhost",
                    Port = 12201,
                    TransportType = TransportType.Udp,
                    Facility = "Test",
                })
                .Enrich.FromLogContext()
                .CreateLogger();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search