Logging with .NET Core
  • 2 Minutes to read
  • Dark
    Light
  • PDF

Logging with .NET Core

  • Dark
    Light
  • PDF

Article Summary

Step 1: Add StackifyLib nuget package and basic configuration

No matter which logging framework you are using, you need to add StackifyLib and add the Stackify.ApiKey to your config file.

Set Stackify Configuration settings

Because .NET core allows a wide variety of configuration options, how you configure the key could vary wildly. This is just one example of how to do it with the standard appsettings.json file.

{
 "Logging": {
   "IncludeScopes": false,
   "LogLevel": {
     "Default": "None",
     "System": "None",
     "Microsoft": "None",
     "Microsoft.AspNetCore.Mvc.Internal":  "None" 
   }
 },
 "Stackify": {
   "ApiKey": "your key goes here",
   "AppName": "CoreWebApp", //optional
   "Environment": "Dev" //optional
 }
}

ASP.NET Core

For ASP.NET you also want to add an additional StackifyLib.AspNetCore nuget package. It enables capturing web request details for exceptions. It is not required, but highly recommended. In your Startup.cs Configure() event you need to add app.ConfigureStackifyLogging(Configuration); as shown here. This assumes that in the Startup constructor method you are also creating your configuration object.

using StackifyLib;
...
public IConfigurationRoot Configuration { get; }

public Startup(IHostingEnvironment env)
{
	var builder = new ConfigurationBuilder()
		.SetBasePath(env.ContentRootPath)
		.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
		.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
		.AddEnvironmentVariables();
	Configuration = builder.Build();

	
	StackifyLib.Config.Environment = env.EnvironmentName; //optional
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
	app.ConfigureStackifyLogging(Configuration); //This is critical!!
	app.UseMvc();
}

Using with a Console application

There is no Startup class like there is for ASP.NET and we aren't worried about collecting web request details. So the configuration is slightly different and there is no need for the extra nuget package.

var builder = new ConfigurationBuilder()
	.SetBasePath(env.ContentRootPath)
	.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
	.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
	.AddEnvironmentVariables();

Configuration = builder.Build();
Configuration.ConfigureStackifyLogging(); //This applies the settings

Step 2: Add your logging provider library

Standard logging libraries Stackify supports the standard third party libraries: log4net, NLog, Serilog For all of these there is a corresponding nuget package required to setup the logging appender/target/provider. Please review these related docs for how to install and configure them. Please note that any reference in them to configure the Stackify.ApiKey and such is specific to non .NET Core, that part needs to be done as mentioned here.

Using the New .NET Core Logging API

For .NET Core you can also use the new logging API that is based around ILoggerFactory. We would recommend also checking out this blog post on the topic: .NET Core LoggerFactory: How to use it correctly and lots of tips!

NOTE: If you are using one of the standard third party libraries, there is no reason to do this also!

Install the nuget package called StackifyLib.CoreLogger and modify your Startup class to register the provider. For an ASP.NET app you also still need the StackifyLib.AspNetCore package as well.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
	loggerFactory.AddStackify(); //add the provider

	app.ConfigureStackifyLogging(Configuration); //configure settings and ASP.NET exception hooks

	app.UseMvc();
}

Step 3: Log some stuff!

If you are still having issues logging to Retrace, please follow the Troubleshoot: Errors and Logs .NET Configurations guide to address common issues.


Was this article helpful?