Using Nlog Logging Framework With Vnext In Asp.Net
In this article, asp.net development company experts will brief you about asp.netVNext and its features. They will also explain how to use NLog Logging framework in Asp.net. Read this article and learn what they want you to understand.
Overview of Asp.Net VNext
The next level of Asp.Net after Asp.Net 5 is Asp.Net VNext. Currently, the code is run in Asp.Net with same CLR as that of desktop apps. There is a need for cloud optimized version of Asp.Net for getting higher throughput with lesser memory use.
Asp.Net will allow programmers to deploy the .Net framework version on the basis of apps. An app having new libraries will not will not break another app of a different version. It is also possible for apps to have the CLR specific to their version. Nuget packages are nothing but cloud optimized libraries and the CLR.
As shown in the above figure, both build 418 and 420 are in the folder 'My packages'. The entire core CLR and the .Net framework are included in the Nuget package. Both of these can be deployed as a Nuget with the app.
Features of VNext
The main features of VNext includes:
- It is cloud optimized as well as server optimized.
- The concept of dependency injection is out.
- Nuget is everything including the runtime.
- Both runtime and framework will be deployed side by side with the application.
- Asp.Net VNext will run of Mac, Linux as well as Mono.
Background for NLog
In the early times, the software was based on the console mostly and no debuggers were there. Tracing messages were written using the printf() command. Now there has been huge enhancement and programmers are using Console.WriteLine() instead of printf() statements. The statements written using Console.WriteLine() are known as tracing statements since their main function is to control the flow of application.
After doing some sort of testing, it was recommended to remove the trace code to improve the performance of application. All tracing code is commented and when required it is re-enabled. However, this approach required recompilation of the program. After some time, there was need to get the perfect tracing solution so that the programmers could get the following benefits:
- Users should have control over the details of different trace messages such as display only warning messages or detailed trace message.
- Users should have access to turn the tracing Off or On for separate program components as required without any need to turn the entire application on and Off.
- Users should be able to write tracing messages to a system log or a message queue or file or any other output as required.
- Users should be able to store important messages in database or send them by mail.
The use of tracing solutions is limited in the era of graphical debuggers. Sometimes, users have only tracing option available to find an error in a critical application.
NLOgis a library of Asp.Net that allows programmers to include tracing code in an application in a sophisticated manner. NLog helps in specifying rules to control the tracing flow from source to target. A source or a target may be:
- Any file
- A text console
- An email message
- A network machine
- Message queue based on MSMQ
- Event logs etc.
NLog also makes it possible to add additional contextual information to the tracing message that will be sent along to the target. The information include the name of source, stacktrace, the environment variables, process, machine, exceptions etc. Every trace message has an associated log level to describe the severity of the message.
NLog Security Levels:
The following security levels are supported by NLog:
- Debug:These include the messages with less detail and these are not very frequent.
- Trace:Theses included detailed message that occurs frequency.
- Warn:These include the warning messages that are not shown t the user.
- Info: These includes the informational messages.
- Error: These includes the error messages.
- Fatal: These includes fatal message. Usually, the application terminates after such type of message.
NLog and its binaries can be downloaded from its website. It is provided with a graphical installer that allows users to install NLog in a specific state for integration with the Visual Studio including the following:
- File Templates configuration.
- Intellisence for the config files of NLog
- The code snippet.
- Dialog integration of add reference dialog.
How to use VNext With NLog:
It is possible to implement Asp.Net VNext with NLog.
Refer to the following steps for using Asp.Net VNext with NLog:
- Create a web project in Asp.Net VNext.
- Open the file "appsettings.json". It will appear as soon as an application will be created in Asp.Net VNext.
- Add the config section of NLog in this file.
- Next, add the following references to the project.json file.
- Open the appsetting.json file and read the config section of NLog for its configuration.
- Deserialize the content from appsetting.json file of NLog config in a class.
- Now it is required to inject factory in controller for logging purpose.
Asp.Net VNext has a very little support for NLog. However, implementation of NLog with Asp.Net VNext is easy because of the Microsoft.Extensions.Logging.Nlog that uses an ILogger for logging purposes. Also, there can be many targets for NLogConfig section including database, file etc. In order to use multiple targets, it is required to add settings in app.config file in the nLogConfig section. Similar structure should also be following in the deserialized class.
Experts of asp.net development company have shared basics of asp.netVNext and its features along with the use of NLog Logging framework. In case you have not get any point or have query, ask in comments.
The image is authorized to publish here by Sina Weibo user @雪狼湖行于世.