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:

  • Count on an unlimited, dedicated SMTP email server included with your service.
  • Apply updates easily 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 widely used and highly extensible logging library, popular for managing logs in .NET applications. It enables developers to configure various sinks for log storage and visualization.

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 following code snippet. 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 without changing any other settings.

To enhance security, it is recommended to restrict which IPs 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 its logs securely and efficiently to your Graylog instance.