在 ASP.NET 的 Application Insight 中设置默认日志严重性

问题描述 投票:0回答:1

我在

.net framework 4.8
中有一个应用程序,我正在使用 Application Insight 和
TelemetryClient
进行日志记录。如何设置应用程序的默认日志严重性。 我知道在 .net core 中我们可以像这样在 appsettings.json 中设置它

"Logging": {
  "IncludeScopes": false,
  "ApplicationInsights": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "LogLevel": {
    "Default": "Warning"
  }
}

但是我们怎样才能在

.net framework
中做同样的事情呢? 这是我的代码示例

public class MyLogger
{
    private static TelemetryClient _telemetryClient;
    public MyLogger()
    {
     _telemetryClient = new TelemetryClient();
    }

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

  public void Information(string message)
  {    
     _telemetryClient.TrackTrace(message, SeverityLevel.Information);
  }
}
c# asp.net azure-application-insights
1个回答
0
投票

您设置严重性日志的方式是正确的,

是的,我们可以在类文件中设置严重性日志级别,然后在需要的地方调用该方法。

我创建了一个 ASP .NET Web 应用程序 (.NET Framework 4.8),为其配置了 Application Insights,并在 Application Insights 中成功观察到了日志。

  • 就我而言,我创建了一个

    MyLogger.cs
    文件,在其中定义了记录具有不同严重性级别的消息的方法。这些方法负责指定严重性级别并相应地记录消息。

  • 在我的

    HomeController.cs
    中,我只是从
    MyLogger.cs

    调用了适当的日志记录方法
  • 通过在

    MyLogger.cs
    文件中设置严重性级别,就不需要每次调用日志记录方法时都指定严重性级别。

这就是我将日志发送到 Application Insights 的方式:

我已将连接字符串添加到我的 ApplicationInsights.config 文件中:

<ConnectionString>Enter your connection</ConnectionString>

这是我的MyLogger.cs:

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
namespace Appinsightstest1
{
    public static class MyLogger
    {
        private static readonly TelemetryClient telemetryClient = new TelemetryClient();
        public static void LogTrace(string message)
        {        
            var telemetry = new TraceTelemetry(message, SeverityLevel.Information);
telemetryClient.TrackTrace(telemetry);
        }
        public static void LogError(string message)
        {       
            var telemetry = new TraceTelemetry(message, SeverityLevel.Error);
                       telemetryClient.TrackTrace(telemetry);
        }
        public static void LogWarning(string message)
        {
            var telemetry = new TraceTelemetry(message, SeverityLevel.Warning);
            telemetryClient.TrackTrace(telemetry);
        }

        public static void LogCritical(string message)
        {
            var telemetry = new TraceTelemetry(message, SeverityLevel.Critical);            telemetryClient.TrackTrace(telemetry);
        }
    }
}

我的HomeController.cs

using Microsoft.ApplicationInsights;
using System.Web.Mvc;
namespace Appinsightstest1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            MyLogger.LogTrace("This is an Information message from the Controller.");
            MyLogger.LogError("This is an Error message from the Controller.");
            MyLogger.LogWarning("This is a Warning message from the Controller.");
            MyLogger.LogCritical("This is a Critical message from the Controller.");

           return View();
        }
    }
}

应用洞察中的痕迹:

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.