RHarris
RHarris

Reputation: 11187

System.Diagnostics.Trace vs. ILogger

I'm confused about the difference between using System.Diagnostics.Trace Trace.TraceInformation("This is a test") and using the ILogger logging abstraction logger.LogInformation("This is a test").

I'm working in a .Net Core 2.2 project and plan to deploy this as an Azure Web App. Is there a difference in the two approaches? Is one better suited for what I'm doing than the other? Why would one use one over the other?

Upvotes: 6

Views: 2949

Answers (3)

James Crosswell
James Crosswell

Reputation: 786

The Microsoft docs on .NET Logging APIs provide a fairly good summary of both (plus EventSource). Summarizing:

ILogger

  • Newer API
  • Supports Structured logging
  • Provides abstractions around third party logging implementations
  • Basis for tooling like OpenTelemetry that's being built out currently

Trace

  • The oldest logging APIs provided for .NET
  • No support for structured logging

Upvotes: 0

diegosasw
diegosasw

Reputation: 15634

The main difference is that ILogger is not part of the framework (netstandard or .NET 5+), it is a higher level logging/tracing framework. System.Diagnostics is part of the .NET framework, out of the box.

Other differences are that ILogger can be used with dependency injection.

Also, there is this

Upvotes: 2

Shridhar R Kulkarni
Shridhar R Kulkarni

Reputation: 7063

Apparently, there is no demarcation which to use when. If you ask yourself "what is the intention?" and the answer happens to be "interacting with system processes, event logs, or performance counters" go for System.Diagnostics. If the answer happens to be "I just want application logging", then go for Logger.

For Azure Web App, you might want to use AddAzureWebAppDiagnostics.

Difference between Logging and Tracing?

The way I stay clear about it is Logging helps you with reporting out any errors.

But if you want to understand which part of a function is causing the error or any performance bottleneck, then use Tracing. Tracing helps to understand how the flow of control or program has been. You might also want to see other levels of logging like fatal, warning, debug, info, etc.

Upvotes: 1

Related Questions