Graylog: Using with Dot NET

How to send logs from .NET/Serilog to Graylog

👋 Welcome to Stackhero documentation

Stackhero offers a fully managed Graylog cloud solution designed for speed and simplicity. You can:

  • Rely on an unlimited, dedicated SMTP email server included with your service.
  • Apply updates effortlessly with a single click, with no manual intervention required.
  • Use a custom domain name secured by HTTPS (for example, https://logs.your-company.com), providing your team with secure, direct access.
  • Benefit from strong performance and security on a private, dedicated infrastructure, with no shared resources or noisy neighbours.

Focus on your data, not your tools: you can get started with Stackhero's Graylog cloud hosting solution in just a few minutes.

Serilog is a highly popular and extensible logging library, widely adopted for managing logs in .NET applications. It enables developers to configure various sinks for log storage and visualisation.

Here is a simple example demonstrating how to use Serilog:

var log = new LoggerConfiguration()
    .WriteTo.Console()
    .WriteTo.File("log.txt")
    .CreateLogger();

log.Information("Hello, Serilog!");

To send logs to a Graylog server, you can use the serilog-sinks-graylog package. This sink allows you to transmit logs in the GELF format directly to Graylog.

  1. Install the package:

    Add the serilog-sinks-graylog package to your project by running the following command:

    Install-Package serilog.sinks.graylog
    
  2. Configure Serilog for Graylog:

    Update your application configuration with the code below. Replace <XXXXXX>.stackhero-network.com with the domain of your Graylog instance:

    var loggerConfig = new LoggerConfiguration()
        .WriteTo.Graylog(
            new GraylogSinkOptions
            {
                HostnameOrAddress = "<XXXXXX>.stackhero-network.com",
                Port = 12201
            }
        );
    
  3. Set up Graylog:

    • Log in to your Graylog dashboard.
    • Go to System > Inputs.
    • Create a new input of type "GELF UDP".
    • Click "Launch new input". In the window that appears, enable the "Global" option, assign a name to the input, and save the settings without making further changes.

To improve security, it is recommended to restrict which IP addresses are allowed to connect to port 12201. You can configure this from the Stackhero dashboard by selecting your Graylog service and managing the "Firewall" settings to allow only specific IPs.

By following these steps, you enable your .NET application to send logs securely and efficiently to your Graylog instance.