Azu*_*aur 5 c# logging azure-application-insights .net-core
ILogger 接口只有一个 Log 方法:
void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter);
Run Code Online (Sandbox Code Playgroud)
应用洞察为我提供了一个丰富的 api,如下所示:
TrackMetric(string name, double value, IDictionary<string, string> properties = null);
TrackException(Exception exception, IDictionary<string, string> properties = null, IDictionary<string, double> metrics = null);
TrackTrace(string message, SeverityLevel severityLevel, IDictionary<string, string> properties);
Run Code Online (Sandbox Code Playgroud)
我应该如何实现自定义 ILogger 并仍然拥有应用程序洞察的所有功能?我能想到的一种方法是定义不同的TState类型,如下所示:
class Trace
{
public string Message {get;set;};
public IDictionary<string, string> Properties;
public Trace(string message, IDictionary<string, string> properties)
{
this.Message = message;
this.Properties = properties;
}
}
Run Code Online (Sandbox Code Playgroud)
然后当我需要跟踪时,我会执行以下操作:
logger.Log<Trace>(LogLevel.Information, eventId, new Trace("sample trace",null));
Run Code Online (Sandbox Code Playgroud)
通过这种方式,我根据类型切换trackXXX我在Log<TState>实现中使用的方法TSTate(我为异常、度量和事件创建类型)。但这看起来太复杂了,无法编写简单的跟踪,我还缺少其他任何方法吗?
当您使用适用于 ASP.NET Core 的 Application Insights SDK并启用 Application Insights 时,将创建内部 ILogger 实现。您可以在这里查看它是如何工作的。
因此,为了回答您的问题,如果您使用适用于 ASP.NET Core 的 AI SDK,则不需要实现自己的 ILogger。但是,如果必须,您可以使用链接的实现作为示例。
| 归档时间: |
|
| 查看次数: |
7708 次 |
| 最近记录: |