July 11, 2024, Abdush Miah
In this tutorial we delve into instruction on how you can add logging to your blazor client side web assembly application.
Add the following nuget package to your application:
Microsoft.Extensions.Logging.ConfigurationAdd the following custom logger configuration in your appsettings.json file:
"Logging":{
"LogLevel":{
"Default": "Information"
}
}
In your program.cs file add the following:
builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));
Next in your .razor file add this to the top of your file in which you'd like log information:
@inject ILogger<MyComponent> Logger;
Now in either your code behind file and or your @code{}
block you can do the following to start logging
public string SomeMethod(){
try{
// some cool stuff happening here
Logger.LogInformation("Just some information to log"); /** Logs information as it all went well **/
}catch(Exceptopm ex){
Logger.LogError($"Some important error which could help troubleshoot, details of the exception {ex}"); /** Logs error as something terrible happened **/
}
}