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.
- Customizable 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 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.
Step-by-step guide
-
Install the package:
Add the
serilog-sinks-graylogpackage to your project by running the following command:Install-Package serilog.sinks.graylog -
Configure Serilog for Graylog:
Update your application configuration with the following code snippet. Replace
<XXXXXX>.stackhero-network.comwith the domain of your Graylog instance:var loggerConfig = new LoggerConfiguration() .WriteTo.Graylog( new GraylogSinkOptions { HostnameOrAddress = "<XXXXXX>.stackhero-network.com", Port = 12201 } ); -
Set up Graylog:
- Log in to your
Graylogdashboard. - 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.
- Log in to your
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.