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 namedAxiomLogs
.
Install packages
Install the packages for your project. Use theMicrosoft.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 namedAxiomLogger.cs
in your project directory with the following content:
Replace
API_TOKEN
with the Axiom API token you have generated. For added security, store the API token in an environment variable.Replace DATASET_NAME
with the name of the Axiom dataset where you send your data.Replace AXIOM_DOMAIN
with api.axiom.co
if your organization uses the US region, and with api.eu.axiom.co
if your organization uses the EU region. For more information, see Regions.Configure the main program
Now that the Axiom logger is in place, update the main program so it can be used. Open theProgram.cs
file and replace its contents with the following code:
Project file configuration
Ensure youraxiomlogs.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: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 theSerilog
, Serilog.Sinks.Http
, Serilog.Formatting.Elasticsearch
and Serilog.Formatting.Json
packages.
Configure Serilog
In yourProgram.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.
Replace
API_TOKEN
with the Axiom API token you have generated. For added security, store the API token in an environment variable.Replace DATASET_NAME
with the name of the Axiom dataset where you send your data.Replace AXIOM_DOMAIN
with api.axiom.co
if your organization uses the US region, and with api.eu.axiom.co
if your organization uses the EU region. For more information, see Regions.Project file configuration
Ensure youraxiomlogs.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:Option 3: Using NLog
Install NLog Packages
You need NLog and potentially an extension for HTTP targets.Configure NLog
Set up NLog by creating anNLog.config
file or configuring it programmatically. Here is an example configuration for NLog
using an HTTP target:
Replace
API_TOKEN
with the Axiom API token you have generated. For added security, store the API token in an environment variable.Replace DATASET_NAME
with the name of the Axiom dataset where you send your data.Replace AXIOM_DOMAIN
with api.axiom.co
if your organization uses the US region, and with api.eu.axiom.co
if your organization uses the EU region. For more information, see Regions.Configure the main program
Update the main program to useNLog
. In your Program.cs
file:
Project file configuration
Ensure youraxiomlogs.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: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