简体   繁体   中英

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM