Default Logging in Visual Studio
This is part 61 of the ASP.NET Core tutorial. In this video, I'll introduce logging capabilities in ASP.NET Core. Before we write any logging code or configuration, let's look at the default logging that we get out of the box with ASP.NET Core.
To understand that, I'm going to run this project that we've been working with so far in this video series in debug mode by clicking this Run button. There we go, we have our application up and running.
Now, if we take a look at the output window within Visual Studio—by the way, if the output window is not present here for you, to open this, click on Debug > Windows and then select Output. Notice within the output window here, we have a lot of information dumped by the process dotnet.exe, and if we scroll all the way down, again, we have a lot of messages here related to "thread has exited with code 0".
At the moment, we are running our project using the out-of-process hosting model. So if we take a look at our project file, notice we are using out-of-process. So the process that is dumping all this information here is dotnet.exe. If you have used the in-process hosting model, then instead of dotnet.exe, you would see IISExpress.exe dumping all this information right here.
To prevent all these messages being logged to the output window within Visual Studio, we can click on Tools > Options. In this Options window, collapse the Environment tab, expand Debugging, and then select Output Window. I am going to turn off all these messages being logged to the output vendor. Click OK.
Stop debugging and then let's run the project again in debug mode. Now if we take a look at the output window again, notice we don't have anything logged here. Let me stop debugging again.
Configuring Log Levels in appsettings.json
In our application configuration file, that is, in this appsettings.json, I'm going to include another log level setting. These log level settings are related to logging configuration. We'll discuss log level in detail in our upcoming videos. For now, I'm going to include another category here, Microsoft, and set this to Information log level.
With this setting in place, we are basically saying all the information logs and higher that are coming from the Microsoft category must be logged and displayed. We'll discuss this log level configuration in detail in our upcoming videos. For now, let's run our project in debug mode again and see the behavior that we get.
If we now take a look at the output window, notice we have a lot of information logged. The first part here that we see is the category of the log, and notice it starts with the word Microsoft. And this is an information log, and the log message is "request starting." That's because we basically said we want all the information level logs and higher from the Microsoft category. And we see several logs here, including the SELECT query that is issued by Microsoft Entity Framework Core to retrieve the list of employees that we see on this web page.
Logging to the Console with dotnet run
At the moment, we are running this project from Visual Studio in debug mode and we see all these log messages displayed in the debug output window. ASP.NET Core is cross-platform, and we can run this same project even from the command line. So let's stop debugging that in Visual Studio and then launch Command Prompt as an administrator.
In the Command Prompt window, change the path to the folder that contains your project. In my case, the project is present in C:\Projects folder. This solution folder is EmployeeManagement. Within that, we have another folder with the same name, EmployeeManagement, which is our project folder right here.
Run the command dotnet run to run our dotnet project. We have our application up and running, and it's listening for incoming requests at this URL: localhost:5000. So in the browser address bar, let's navigate to localhost:5000. There we go, we have our list of employees.
Now, if we take a look at the console window, notice we have a lot of information logged. Just like the debug window in Visual Studio, the category name here starts with the word Microsoft, the log level is info, and we also have the SQL query issued by Entity Framework Core to retrieve the list of employees.
Understanding Logging Providers and CreateDefaultBuilder
Now, the point that I'm trying to make is we did not write any code, except this one line of configuration right here, to have these logs displayed on the console window if we are running the project from the command line, and in the debug output window if we are running the project from Visual Studio in debug mode. So how are these logs displayed?
Well, we have something called logging providers, and these logging providers physically store or display logs. For example, we have a console logging provider. This provider displays logs on a console if we are running the project from the command line. Similarly, we have a debug logging provider. This provider displays logs on the debug window in Visual Studio if we are running the project in Visual Studio in debug mode.
Now, in our previous videos in this series, we discussed the significance of the Main method in this Program.cs file. It is the entry point into our ASP.NET Core application. This method calls CreateWebHostBuilder, which in turn calls CreateDefaultBuilder. This method performs several tasks while starting our ASP.NET Core application. It is this method that sets up the web server, loads our application configuration information from various configuration sources, and it is also responsible for configuring logging.
At the moment, we are on the official ASP.NET Core GitHub page. Using the search box that we have right here, I have searched for this CreateDefaultBuilder method in this repo. And at the moment, we are looking at WebHost.cs file, and if we scroll down, notice we have the CreateDefaultBuilder method right here. And as you can see, this method configures our application configuration. And if we scroll down a bit further, notice it also configures logging. As part of this logging configuration, notice it's looking for logging-related settings in a section called Logging. So if we take a look at appsettings.json file, notice we have a section with name Logging, and inside that, we have a nested LogLevel object. We'll discuss log levels in detail in our upcoming videos.
In addition to reading the logging-related configuration information from the Logging section, it also adds these three logging providers: Console, Debug, and EventSource to our application out-of-the-box.
Built-in and Third-Party Logging Providers
This is the reason we are able to see the logs displayed in the console window and in the debug output window in Visual Studio. In addition to these three logging providers—Console, Debug, and EventSource—there are several other built-in logging providers provided by ASP.NET Core out of the box.
As we already discussed, these different logging providers allow us to send logs to different destinations. For example, Console displays logs on the console window. If you're on a Windows operating system and you want to log the messages to the Windows Event Log, then use the Event Log provider. Similarly, Azure App Services File provider logs messages to the Azure App Services file, and similarly, this provider logs messages to Azure App Services blob storage.
In addition to these built-in logging providers provided by ASP.NET Core, we also have several third-party logging providers. We have, for example, NLog, Serilog, etc. In our upcoming videos in the series, we'll discuss how to log warnings and exceptions from our application to a file using this third-party logging provider, NLog.
This is just an introduction to logging capabilities in ASP.NET Core. There's a lot more to learn. We will cover most of the logging concepts in our upcoming videos in this series, so please stay tuned. Thank you for listening. You.