diagnostics icon indicating copy to clipboard operation
diagnostics copied to clipboard

Add new tool for tailing logs

Open shirhatti opened this issue 7 years ago • 12 comments

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:

  1. Ability to tail logs without the need to modify existing user code.
  2. Support configurable (and reloadable) log verbosity

shirhatti avatar Oct 08 '19 06:10 shirhatti

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.

davidfowl avatar Oct 08 '19 07:10 davidfowl

I assume there'll also be a way from command line options to change categories? Or is that only from a file?

davidfowl avatar Oct 08 '19 07:10 davidfowl

https://github.com/shirhatti/dotnet-logs

shirhatti avatar Oct 08 '19 09:10 shirhatti

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)

sywhang avatar Oct 08 '19 23:10 sywhang

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

shirhatti avatar Oct 09 '19 01:10 shirhatti

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!

nblumhardt avatar Oct 12 '19 23:10 nblumhardt

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).

davidfowl avatar Oct 12 '19 23:10 davidfowl

@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.

davidfowl avatar Dec 13 '22 08:12 davidfowl

Found it https://github.com/dotnet/diagnostics/issues/539#issue-503063265!

davidfowl avatar Dec 13 '22 08:12 davidfowl

@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

tommcdon avatar Dec 13 '22 20:12 tommcdon

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.

dramos020 avatar Jan 10 '23 19:01 dramos020

Oh yeah

davidfowl avatar Jan 10 '23 19:01 davidfowl