ASP NET Core LogLevel configuration

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
they say spot 64 of asp.net core tutorial in this video we'll discuss log level configuration in asp.net core with examples log level indicates the severity of the logged message it can be any of the following trace debug information warning etc these options are listed here from the lowest to highest severity to take a look at the log level genome and all of its options let's flip over to visual studio at the moment I am in the home controller let's type log level here log level is in Microsoft or extensions dot login namespace so let's bring that in and now if I right-click on this and then go to definition notice we see the same values here that we see on a slide starting with trace with the value of zero all the way till none with a value of six one place where we used this value as a setting is an AB setting stored JSON file notice at the moment for both the categories default and Microsoft we have set log level two warning for now for this default category let's change the log level to trace next let's inject the built-in ilogger service provided by asp.net core into our home controller using constructor injection let's call the parameter logger and let's also generate the corresponding field by pressing control period we don't need this log level you know here in the constructor so let's get rid of that and then in the detail section method let's comment this line that is throwing an exception these are the log level enum options starting from trace all the way till none now from our application if we want to log a trace message then on the ilogger service that we have injected into our home controller we have log trace method similarly to log a debug message we have log debug method for information log information law warning log error etc so the point that I'm trying to make is for every log level option that we have here we have a corresponding log method on the I log a service with the exception of this last option none on this I log a service instance if we type dot and then log notice I see methods like log critical debug era etc for example if I want to log a trace message I could use log trace similarly to log a debug message we use log debug method in the interest of time let me paste four more lines of code notice to log an information message we're using log information method for warning a using log warning so at the moment we are logging messages from the lowest severity that is trace to the highest severity critical using the corresponding log methods that we have available on the ilogger service instance so with all these changes let's run our project in debug mode and see the log output that we get notice we see our custom error view instead of the list of employees this means there was an error processing of a request on the server so if we take a look at the debug output window in Visual Studio we have an error logged by our error controller there is an exception and the exception type is invalid operation exception and if we look at the exception message unable to resolve service for type Microsoft or extensions dot logging or ilogger while attempting to activate our home controller so basically within our home controller we are injecting a logger service but we forgot to specify the generic argument so we are logging from home controller so let's specify the home controller as the argument for the generic parameter of our ilogger service and then run the project again in debug mode there we go we now have the list of employees as expected at the moment we have the log statements in the details action method of our home controller so let's navigate to slash home slash details by clicking this view button we see the employee details as expected and now if we take a look at the debug output window notice we have all the messages that we have logged from the lowest level trace to the highest level critical and that's because if we take a look at app settings dot Jason for the default category we have set a log level to trace by setting the log level to trace we're basically saying we want all the trace level and higher severity messages so we see everything from trace to critical on the other hand in the log output vendor notice we do not see any log messages from the Microsoft category that's because we have set its log level to warning so basically we are saying from the Microsoft category we only want to see warning level and higher severity messages but at the moment from the Microsoft category we do not have any such messages and that's the reason we don't see anything logged from this category Microsoft if you want to see all the information and higher severity logs from Microsoft then change the log level here to information if we do that we see lot of information logs from Microsoft namespace we don't want that to happen that's why we have set it to warning now let's look at another example before that let's stop debugging let's open sequel employee repository class from the models folder now let's inject the ilogger service into this sequel employee repository class as well let's bring in the require namespace and specify sequel employee repository class type as a generic argument let's call the parameter logger and let's also generate the private view if we scroll down now we have this method cat employee which is called everytime PBU a specific employee details so inside this method I'm going to copy the log statements that we have in the detailed section method of our home controller and then paste those lines right here and then let's run the project in debug mode we see the list of employees at the moment within the debug output window we don't see anything logged now let's view a specific employee details notice now for this category employee management controllers dot home controller we have all the log messages from the lowest level trace to the highest level critical and the same is true even for this other category that we have right here employee management models dot sequel employee repository from the lowest level trace to the highest level critical this is because in app settings dot Jason we have set the log level for default category to trace and the default category is our project namespace if we take a look at the name of our project it is employee management so all the custom classes that we have in our project like this home controller and a simple employee repository their namespace start with the default category which is employee management as you can see here and that is the reason for both these classes home controller and sequel employee repository we see trace level and higher severity messages up until critical now let's take a look at another example here's what we want to do for the home controller we don't want trace debug and information logs we only want warning level and higher severity errors whereas for sequel employee repository category we want all the logs from trace till critical first let's stop debugging for this category employee management dot controllers dot home controller we want trace level and higher so let's copy the category name and then base with an app settings dot Jason I'm going to leave the default category there but then override the setting for this category so for this category we only want warning and higher severity errors so I'm going to set log level two warning let's do the same thing for the other category first copy the category name paste it within app setting star Jason and then choose the log level to trace run the project and let's see the log output that we get view one of the employee details and if we take a look at the log output notice from the home controller category we only have warning level and higher severity errors whereas from the sequel employee repository category we have all the log messages from the lowest level trace to the highest level critical at the moment with a configuration that we have here we're only filtering logs by the log category we can also filter logs by logging provider what's a login provider a login provided is a component that physically stores are displaced logs for example console login provider displays logs on the console window similarly debug logging provider displays logs in the debug output window in visual studio as you can see right here now let's take a look at an example of where we can filter both by category as you can see right here and by logging provider as well so let's make a copy of this log level configuration that we have and then change the best that are required we want this log level configuration to be specific for the debug logging provider for that what I'm going to do is include the login provider name here which is debug and then the value for this is this entire log level object so let's wrap it in another pair of curly braces with the configuration we're basically saying for the debug login provider use this log level configuration whereas for the rest of the logging providers that is for example console logging provider even source event log even for that matter third party logging providers like n log used this log level configuration so with all these changes in place first let's stop debugging before he is on the project again for the debug login provider let's set all these categories to warning and for the other login providers let's set all the categories to trace let's run the project and see the output we get now if we take a look at the debug output window in Visual Studio notice for these two categories home controller and sequel employee repository we only have warning level and higher severity logs that's because we said for the debug log in provider we only want warning and higher severity logs for these two categories whereas if we look at the log level configuration for the rest of the logging providers for example for the console logging provider for these two logging categories we want trace and higher severity logs so let's run this same project from the command line and see the log output that we get before we run this project let's set the Microsoft category log level to warning otherwise we are going to see lot of info logs from the Microsoft namespace on the command line and it would be extremely difficult to see the log messages that are coming from these two categories so with this change in place let's run the project from the command line using dotnet run command there we go we have our project up and running at this URL localhost colon 5,000 but I still see info log coming from the Microsoft namespace let's understand why is this happening if you recollect from our previous videos in the series the configuration in setting star Jason can be overridden by the configuration in environment specific app settings dot JSON file at the moment they're running this project from the command line when we run the project from the command line it uses this profile that we have in launch settings dot JSON file and within this profile we have said a spinet core environment variable to development so at the moment we're running this project in the development environment so asp.net core in addition to looking at the configuration in app settings Jason it is also going to look in the environment specific app settings dot Jason in this case it looks for app settings dot development or Jason because we are running the project in the development environment so if we take a look at this app settings dot development dot JSON file notice for the Microsoft category log level is set to information and that's the reason we see the info log from the Microsoft category let's swap the project change the log level to warning save changes and then run the project again from the command line notice now we don't see the info log from the Microsoft category or project is up and running at localhost 5000 so let's navigate to localhost 5000 there we go we see the list of employees and on the console window we still to not see any info logs from the Microsoft category which is good now let's navigate to view a specific employee details at this point notice from the home controller category we have all the log messages from the lowest level trace to the highest level critical and the same is true even for the sequel employee repository category from the lowest level trace to the highest level critical we have all the log messages in asp.net core locks can be filtered by low category logging provider or even both here is that same example which we just looked at action that said in this video thank you for listening [Music]
Info
Channel: kudvenkat
Views: 41,116
Rating: undefined out of 5
Keywords: asp.net core loglevel appsettings.json, asp.net core loglevel configuration, asp.net core change log level, asp.net core console log level, asp.net core log level example, asp.net core logging log level, asp.net core logging loglevel, asp.net core loglevel not working, asp.net core set log level, asp.net core logging level tutorial, asp.net core disable console logging, asp.net core disable microsoft logging, asp.net core disable default logging
Id: bTPnT13Efd4
Channel Id: undefined
Length: 15min 45sec (945 seconds)
Published: Fri May 31 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.