Graylog: Using with Dot NET

How to send logs from .NET/Serilog to Graylog

👋 Welcome to the Stackhero documentation!

Stackhero offers a ready-to-use Graylog cloud solution that provides a host of benefits, including:

  • Unlimited and dedicated SMTP email server included.
  • Effortless updates with just a click.
  • Customisable domain name secured with HTTPS (for example, https://logs.your-company.com).
  • Optimal performance and robust security powered by a private and dedicated VM.

Save time and simplify your life: it only takes 5 minutes to try Stackhero's Graylog cloud hosting solution!

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 visualisation.

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.