Add new tool for tailing logs
We should considering adding a new tool (dotnet logs) to our suite of existing diagnostic tools that allows you to connect to and tail logs from an existing process that utilizes Microsoft.Extensions.Logging as the logging library.
While it is already possible to collect logs via dotnet trace by enabling the appropriate provider, there is no way to immediately consume these logs for ad-hoc analyses. Similar to dotnet counters, this tool will enable ad-hoc investigations by providing a textual-output to the console.
All ASP.NET Core/Worker application in 3.0 have an EventSource sink for ILogger already wired up in the default host and should benefit from this without the need to instrument the end user's code.
Goals with this tools:
- Ability to tail logs without the need to modify existing user code.
- Support configurable (and reloadable) log verbosity
Usage:
dotnet-logs [options] [command]
Options:
--version Display version information
Commands:
collect Collects a logs from a currently running process
monitor Start monitoring a .NET application.
ps Lists dotnet processes that can be attached to.
I assume there'll also be a way from command line options to change categories? Or is that only from a file?
https://github.com/shirhatti/dotnet-logs
I'm guessing the output will be similar to what you might see from doing something like dmesg or tail /var/log? (As in it just spews out the text)
My current prototype preserves the formatting from the M.E.L ConsoleLogger.
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 GET http://localhost:5000/
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
Executing endpoint '/ HTTP: GET'
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
Executed endpoint '/ HTTP: GET'
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished in 60.6041ms 200
Nice! 😎
Any idea how we might be able to hook a Serilog event source into this? Serilog can be configured to write events through MEL providers (which I think will cover the MEL EventSource provider), but there's some friction/inefficiency in that path, so having the option to go directly from a Serilog EventSource to dotnet logs would be awesome.
We haven't started on a Serilog EventSource yet (thanks for the nudge @davidfowl 😄) so we can structure the event source according to whatever pattern this tool requires, if that would help.
For example, if we published identical events/keywords/etc. to the MEL EventSource, would it be as simple as adding an additional "Serilog" provider to the list here?
https://github.com/shirhatti/dotnet-logs/blob/dacfa61524ca96bd08b7dae5ffcce9a0f8a92954/src/dotnet-logs/LogViewerService.cs#L90
If we all follow the same event schema, adding "NLog" and even perhaps "log4net" well-known provider names to this list could be a low-friction option.
Happy to spike up the Serilog EventSource, and test/PR the addition to your prototype, if you think this is the right direction. Cheers!
There's 2 models we could chase:
- Plug in logger providers out of process.
- Plug it in in process.
The way this tool works is by hooking up to to the LoggingEventSource, configuring the appropriate filters, getting events, then logging those log messages in the remote process using the ILogger abstraction. What's neat about this is that there's a potential for the tool itself to load other ILoggerProviders and it would just work.
In the in process model, we would define a contract for LoggingEventSource (or make that the contract) and the tool would hook into that. I'm not sure what the benefit of that would be though (but I haven't thought about it much).
@tommcdon I see this in .NET 8 but I think there might be a more pressing need and that's documenting how to collect logs using dotnet trace. I thought we had it documented but I can't find it anywhere but the source code.
Found it https://github.com/dotnet/diagnostics/issues/539#issue-503063265!
@tommcdon I see this in .NET 8 but I think there might be a more pressing need and that's documenting how to collect logs using dotnet trace. I thought we had it documented but I can't find it anywhere but the source code. Found it #539 (comment)!
@dramos020 would mind taking a look at documenting collecting logs with dotnet-trace? cc @mikelle-rogers
Is the documentation on this page not what we are looking for?: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-7.0 It has a section for using dotnet trace to collect logs.
Oh yeah