Disclaimer: Images and steps used in this example may change over time, but may be a good reference.
Short answer: Make logging great again
Slightly longer answer: Make the application send performance-data to Application Insight. Also make all logging from the frameworks, and logging done by me, automatically be sent to Azure for further analysis.
- NLog - Base package for NLog
- NLog.Web:AspNetCore - Adds helpers and layout renderes for the ASP.NET core platform
- Lightinject - IOC-container
- Lightinject.Microsoft.AspNetCore.Hosting - Enables Lightinject to be used in ASP.NET core applications
- Microsoft.ApplicationInsight.AspNetCore - Official package for working with Azure Application Insight
- Microsoft.ApplicationInsight.NLogTarget - Allowing you to send NLog messages to Application Insight
- First we have to make ourself a Application Insight resource in Azure.
- Fill in information about your application, and give it a descriptive name
- Now we are almost done. Create it, and take note of the Instrumentation Key. We will use it shortly
This is based on the example project in this repo. It is a simple project created using the dotnet cli and this command: dotnet new webapi
.
After the template is bootstrapped we need some base-files and interfaces. Big shoutout to @andmos for providing me with the base logfiles for this project.
File | What it does |
---|---|
ILog.cs |
Is the base log-interface used when logging. |
ILogFactory.cs |
The abstraction of the log-factory. This gets the implemented logger from the IOC-container |
Log.cs |
Gets bootstrapped to log-actions for common Log Levels . |
NLogFactory.cs |
Implements a factory that produces logging based on NLog. |
ILogFactoryInitializer |
An abstraction for the builder-method that adds the logger to the ASP.Net Core-app. |
NLogFactoryInitializer |
Implements the .UseNLog() used in Program.cs to setup NLog for DI. |
Program.cs - gets logger factory initializer (wow. good name. thanks) from container and passes the WebHostBuilder
to the implementet builder-method.
public static void Main(string[] args)
{
var container = new ServiceContainer();
var loggerFactoryInitializer = container.GetInstance<ILogFactoryInitializer>();
var webhostBuilder = CreateWebHostBuilder(args);
loggerFactoryInitializer.UseLogger(webhostBuilder);
webhostBuilder.Build().Run();
}
ValuesController.cs - Added examples of logging. With Log Level
Information and Error.
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
var values = new []{"value1", "value2"};
_logger.Info($"Request made, returning:{JsonConvert.SerializeObject(values)}");
return values;
}
// GET api/values
[HttpGet("ThrowPlease")]
public IActionResult ThrowMePlox()
{
try
{
throw new ArithmeticException("Dividing by 0?? Good luck");
}
catch (Exception e)
{
_logger.Error("This was bad. not good", e);
return BadRequest();
}
}
appsettings.json - contains the Application Insight Key for this project. This can also easily be set as an Enviromental Variable.
The Application Insight framework will automatically search for this path in appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"ApplicationInsights": {
"InstrumentationKey": "<KEY>"
},
"AllowedHosts": "*"
}
Log-entries can be searched and filtered in the
search
-tab in Application Insight. Other performance-data can be found iMetrics
.
# clone the project
git clone https://github.com/simonkaspersen/Logging-to-Application-Insight-in-ASP.NET-Core.git
# enter the project directory
cd Logging-to-Application-Insight-in-ASP.NET-Core
# Add your key to appsettings.json
# install dependencies
dotnet restore
# build solution
dotnet build
# Run
dotnet run
This will open the API at http://localhost:5000.
- Go to
http://localhost:5000/api/values/
to test Information-logging - Go to
http://localhost:5000/api/values/throwplease
to test Error-logging
- Deside if Pepsi Max or Coca Cola No Sugar is best
- Implement NLog
- Implement Log4Net
- Implement Serilog
- @andmos