Logging
We use Serilog
for all logging inside the application. Logging is configured in Startup.InitializeLogging
. Please take a look at that function, as it contains behavior you may want to modify. Here are some of the key concepts.
.Enrich.FromLogContext()
This is a Serilog enricher that enables the use of ambient context logging, such as the current Username. More information available here..WriteTo.File("logs/log.txt", [...])
All logs are also written to disk. You may change the file location if you need to, or remove this altogether. In Azure you can find these logs if you go to the Portal -> Your Service App -> Advanced Tools -> Go -> Debug Console -> Powershell -> site\wwwroot\logs. These are really helpful to debug startup issues..MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
The Microsoft source can be quite verbose, so in Production, make sure this is set to at least Warning. Depending on your case, you may want to set it toLogEventLevel.Fatal
, which will produce virtually no messages. You can play around with these overrides to see what information is being logged and adjust it for your needs.Serilog.Debugging.SelfLog.Enable(writer);
This enables Serilog self logging. These logs contain information about when Serilog writes to the log sinks, how many messages, and so on.loggerConfiguration.WriteTo.AzureAnalytics([...])
If enabled in the configuration, this will send your logs to Azure Analytics.Log.Logger = loggerConfiguration.CreateLogger()
This registers the global logger, allowing you to inject the logger into Blazor pages or Api Controllers.
Azure Analytics
When we were evaluating the various places where we could store the Production logs, we had a few choices: file, database, something else. We didn't want to put it in a file, because it would not be useful. From experience, using a database to do it is not scalable. That left the third option. After careful consideration, we picked Azure Analytics. While it does have a learning curve, you will be better off in the long run. It features powerful log analysis tools and great flexibility. Additionally, it is a very cheap solution and the free tier is quite generous. For more information on how to set this up, please see this article.
Logging in the Application
The first thing you will notice is that we actually do not log anything on any of the system pages. This was a conscious choice we made. We didn't want to pollute your logs, so we decided not to log anything. Feel free to add logging as you see fit in any of the existing pages and controllers.
Writing your own log messages could not be simpler. For a Blazor page, use @inject ILogger<PageName> Logger
and use it as such: Logger.LogError("Oh no!")
. Similarly, in C# code, you can inject ILogger<YourClass> into any class registered with the Dependency Injection container.
Table of Contents
- Initial Setup
- Project Structure
- Configuration
- Identity
- Entity Framework
- Authorization
- Api
- User Interface
- Logging
- Email Service
- Background Workers
- Localization
- Services
- Creating a new Page
- Publishing to Azure
- Upgrading