【问题标题】:ILogger to Application InsightsILogger 到 Application Insights
【发布时间】:2019-07-14 14:06:05
【问题描述】:

Microsoft.ApplicationInsights.AspNetCore v2.6.1 与.net core v2.2.2 一起使用,我可以在Azure Application Insight Live Metric Stream 中看到遥测数据,但我没有看到我尝试在@ 内使用ILogger 记录的条目987654323@ 或在控制器中。

我已经在Program.csStartup.cs 中尝试过.UseApplicationInsights()WebHost.CreateDefaultBuilder,就像这样,但无济于事。

services.AddApplicationInsightsTelemetry( options => {
  options.EnableDebugLogger = true;
});

我看到传入请求和请求失败率,但没有日志条目

this.logger.Log(LogLevel.Error, $"Test Error {Guid.NewGuid().ToString()}");
this.logger.LogTrace($"Test Trace {Guid.NewGuid().ToString()}");
this.logger.LogInformation($"Test Information {Guid.NewGuid().ToString()}");
this.logger.LogWarning($"Test Warning {Guid.NewGuid().ToString()}");
this.logger.LogCritical($"Test Critical {Guid.NewGuid().ToString()}");
this.logger.LogError($"Test Error{Guid.NewGuid().ToString()}");
this.logger.LogDebug($"Test Debug {Guid.NewGuid().ToString()}");

【问题讨论】:

    标签: c# azure asp.net-core logging azure-application-insights


    【解决方案1】:

    更新

    如果你安装了最新的包Microsoft.Extensions.Logging.ApplicationInsights (2.9.1),你可以关注这个doc

    在program.cs中:

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }
    
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .ConfigureLogging(logging=> {
                    logging.AddApplicationInsights("your_insturmentation_key");
                    logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace); #you can set the logLevel here
                });        
     }
    

    然后在controller.cs中:

        public class HomeController : Controller
        {
            ILogger<HomeController> Logger { get; set; }
            TelemetryClient client = new TelemetryClient();
            public HomeController(ILogger<HomeController> logger)
            {
                this.Logger = logger;
            }
    
            public IActionResult Index()
            {
                Logger.LogTrace("0225 ILogger: xxxxxxxxxxxxxxxxxxxxxxxxx");
                Logger.LogDebug("0225 ILogger: debug from index page aa111");
                Logger.LogInformation("0225 ILogger: infor from index page aa111");
                Logger.LogWarning("0225 ILogger: warning from index page aa111");
                Logger.Log(LogLevel.Error, "0225 ILogger: error from index page aa111");
                return View();
            }
    
            # other code
    
         }
    

    测试结果(所有日志都发送到应用洞察):

    【讨论】:

    • 您是否知道 LogError 是否可以映射到 AppInsights 上的异常?编辑:找到我的答案here:如果您使用异常对象登录,它会映射到应用洞察力中的异常遥测。
    • @ivan-yang 是TelemetryClient client = new TelemetryClient(); 必要的吗?
    • @urig,您能告诉我您使用的是哪种网络项目吗? .net core 2.2 还是其他?
    • @urig TelemetryClient client = new TelemetryClient(); 不是必需的。实际上,不建议在 ASP.NET Core 应用程序中创建新的 TelemetryClient 实例。使用构造函数注入。
    • 您是否必须指定您所做的检测密钥才能使其工作?我在配置中设置了它,Program.cs 只有AddApplicationInsights();TelemetryClient 工作(使用 DI),但ILogger 没有发送任何东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    • 2021-09-21
    • 2019-08-27
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    相关资源
    最近更新 更多