Graylog: Using with Dot NET

How to send logs from .NET/Serilog to Graylog

👋 Welcome to Stackhero documentation

Stackhero provides 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 needed.
  • Use a custom domain name secured by HTTPS (for example, https://logs.your-company.com), giving your team secure, direct access.
  • Experience strong performance and security on a private, dedicated infrastructure with no shared resources or noisy neighbors.

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

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

Here is a basic example demonstrating the use of 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 facilitates transmitting logs in the GELF format directly to Graylog.

  1. Install the package:

    Add the serilog-sinks-graylog package to your project by executing 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 your Graylog instance's domain:

    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.
    • Navigate to System > Inputs.
    • Create a new input of type "GELF UDP".
    • Click "Launch new input". In the modal that appears, enable the "Global" option, assign a title to the input, and save the settings without further alterations.

To enhance security, it is advisable to restrict the IPs allowed to connect to port 12201. You can configure this by accessing the Stackhero dashboard, selecting your Graylog service, and managing the "Firewall" settings to permit only specific IPs.

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