Send logs from a .NET app
This guide explains how to set up and configure logging in a .NET application, and how to send logs to Axiom.
Prerequisites
- Create an Axiom account.
- Create a dataset in Axiom where you send your data.
- Create an API token in Axiom with permissions to update the dataset you have created.
Option 1: Using HTTP Client
Create a new .NET project
Create a new .NET project. In your terminal, go to the directory where you want to create your project. Run the following command to create a new console app named AxiomLogs
.
Install packages
Install the packages for your project. Use the Microsoft.AspNet.WebApi.Client
package to make HTTP requests to the Axiom API. Run the following command to install the package:
Configure the Axiom logger
Create a class to handle logging to Axiom. Create a new file named AxiomLogger.cs
in your project directory with the following content:
Configure the main program
Now that the Axiom logger is in place, update the main program so it can be used. Open the Program.cs
file and replace its contents with the following code:
This code simulates various app operations and logs messages at different levels (DEBUG, INFO, WARN, ERROR) to Axiom.
Project file configuration
Ensure your axiomlogs.csproj
file is configured with the package reference. The file should look like this:
Build and run the app
To build and run the app, go to the project directory in your terminal and run the following command:
This command builds the project and runs the app. You see the log messages being sent to Axiom, and the console displays Log sent successfully.
for each log entry.
Option 2: Using Serilog
Install Serilog Packages
Add Serilog and the necessary extensions to your project. You need the Serilog
, Serilog.Sinks.Http
, and Serilog.Formatting.Json
packages.
Configure Serilog
In your Program.cs
or a startup configuration file, set up Serilog to use the HTTP sink. Configure the sink to point to the Axiom ingestion API endpoint.
Project file configuration
Ensure your axiomlogs.csproj
file is configured with the package references. The file should look like this:
Build and run the app
To build and run the app, go to the project directory in your terminal and run the following commands:
This command builds the project and runs the app. You see the log messages being sent to Axiom.
Option 3: Using NLog
Install NLog Packages
You need NLog and potentially an extension for HTTP targets.
Configure NLog
Set up NLog by creating an NLog.config
file or configuring it programmatically. Here is an example configuration for NLog
using an HTTP target:
Configure the main program
Update the main program to use NLog
. In your Program.cs
file:
Project file configuration
Ensure your axiomlogs.csproj
file is configured with the package references. The file should look like this:
Build and run the app
To build and run the app, go to the project directory in your terminal and run the following commands:
This command builds the project and runs the app. You should see the log messages being sent to Axiom.
Best practices for logging
To make your logging more effective, consider the following best practices:
- Include relevant information such as user IDs, request details, and system state in your log messages to provide context when investigating issues.
- Use different log levels (DEBUG, INFO, WARN, ERROR) to categorize the severity and importance of log messages. This allows you to filter and analyze logs more effectively
- Use structured logging formats like JSON to make it easier to parse and analyze log data
Conclusion
This guide covers the steps to send logs from a C# .NET app to Axiom. By following these instructions and adhering to logging best practices, you can effectively monitor your app, diagnose issues, and gain valuable insights into its behavior.
Was this page helpful?