As usual, you can obtain a reference to ILogger<T> from the DI framework and use it in your views, like this:
@inject ILogger<MyView> Logger
But there is also another built-in mechanism, the DiagnosticSource class, and property, which is declared in the RazorPage base class. By calling its Write method, you can write custom messages to a diagnostics framework. These messages can be any .NET object, even an anonymous one, and there is no need to worry about its serialization. Have a look at the following code snippet:
@{
DiagnosticSource.Write("MyDiagnostic", new { data = "A diagnostic" });
}
What happens with this diagnostic message is actually somewhat configurable. First, let's add the Microsoft.Extensions.DiagnosticAdapter NuGet package, and then create a custom listener for the events generated for this diagnostic source, like this:
public class DiagnosticListener
{
[DiagnosticName...