Introduction
NLog is a free open source logging framework for .NET (official site here), started by Microsoft’s Jarek Kowalski. It is extremely powerful, and very easy to setup. And it also happens to be my favorite one, that’s why I am going to use it here today with new and latest ASP.NET vNext RC1 release.
Background
The idea came up with nLog implementation with ASP.NET vNext as many of us will be willing to use nLog with ASP.NET vNext. But since the ASP.NET vNext has many changes from its previous version, hence one finds it difficult to implement nLog and how to deal with configuring the nLog.
Using the Code
Create an ASP.NET vNext solution. After the project is created, observe the file appsetting.json as shown in the below image:
Open the "appsettings.json" and add the nlog config section.
"NLogConfig": {
"Name": "LogFile",
"Type": "File",
"FileName": "Project.Log",
"KeepFileOpen": "false",
"CreateDirectory": "true",
"ConcurrentWrites": "true",
"ArchiveOldFileOnStartup": "false",
"Level": "Info",
"Layout": "[${shortdate} | ${level} | ${logger}] ${message}"
}
Now once this section is added, we need to get the required packages by referencing them in project.json file which is observable in the image above.
Contents of the project.json file shown below:
Now we have to read the nLog config section from appsetting.json file and use it to configure our nlog for logging purposes. Hence, we prepare a class "NLogConfig.cs" as shown in the first image.
The NLogConfig
is used to deserialize JSON content of nlog config from the appsettings.json. The content of NLogConfig
is shown as below.
Now let's open up startup.cs file and configure our nlog. Here, we will be deserializing the nlog config into NLogConfig
object and use it to create configuration for the nLog.
Under the Configure
method of our startup file, we need to put the following code which does 3 things:
- Deserialize the nlog config into
NLogConfig
object.
- Use
NLogConfig
to create the LoggingConfiguration
- Finally, add the nlog to the default
Ilogger
factory which is provided by the Microsoft.Extensions.Logging
framework.
Code for the LoadNLogConfig
is as below:
Now our nLog is completely configured to write the log to files. We need to inject ILoggerFactory
in controller where we wish to log things. The code for logging inside a controller is shown below:
This completes our logging implementation with nLog together with Microsoft.Extensions.Logging
framework.
Points of Interest
The nLog support for ASP.NET vNext is still very minimal. The good thing is Microsoft.Extensions.Logging.Nlog
uses ILogger
internally to log, hence it becomes easy to implement and it works together with as many loggers as you add to the logger factory in the startup class.
Moreover, the NLogConfig
section can have other targets too apart from File like Database and Email. If we wish to use more targets than one has to put the setting under the nLogConfig
section in appsetting.json file and corresponding structure should also be reflected in the deserialized object class.
The nLogConfig
section can also put inside a completely new .json file which will serve the sole purpose of Logging Settings for nLog. If this is the approach you wish to follow, then one has to add this file in services collection similar to the way as the appsetting.json is added and read in startup.cs file.
I have tried to kept this tip as simple as it could be. The tip only depicts one of the many ways to implement the nLog with ASP.NET vNext, there might be different and even better approaches that can always come to any of us. It is just a start up code.