在Application Insight TelemetryClient中控制自定义日志记录

时间:2019-02-14 10:43:27

标签: c# azure azure-application-insights

我使用TelemetryClient将应用程序日志存储在Application Insight中。现在,我想根据环境控制日志记录。

我尝试使用host.json文件设置LogLevel,但是它不起作用。我没有看到TelemetryClient中的任何属性来通过AppSettings传递LogLeve。

public class Log : ILog
{
    private static TelemetryClient telemetryClient = new TelemetryClient() { InstrumentationKey = ConfigurationManager.AppSettings["APPINSIGHTS_INSTRUMENTATIONKEY"] };

    public void Error(string message, Exception ex = null)
    {
        telemetryClient.TrackTrace(message, SeverityLevel.Error);
        if (ex != null)
            telemetryClient.TrackException(ex);
    }

    public void Info(string message)
    {
        telemetryClient.TrackTrace(message, SeverityLevel.Information);
    }

    public void Verbose(string message)
    {
        telemetryClient.TrackTrace(message, SeverityLevel.Verbose);
    }

    public void Warning(string message)
    {
        telemetryClient.TrackTrace(message, SeverityLevel.Warning);
    }
}

host.json

{  "logger": {    "categoryFilter": {      "defaultLevel": "Information"     }}

功能

public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]HttpRequestMessage req,
        ExecutionContext context, [Inject] ILog log)
    {
        log.Verbose("verbose log");
        log.Info("info log");
        log.Error("error log");
        log.Warning("warning log");
        return req.CreateResponse(HttpStatusCode.OK, "Okay");
    }

ApplicationInsight

我不想详细登录Prod env。

enter image description here

0 个答案:

没有答案