简体   繁体   English

将 serilog 与 azure application insights 和 .Net core 结合使用

[英]Using serilog with azure application insights and .Net core

Currently, I am using azure application insights directly for logging as given in this link Use latest version of Application Insight with .net core API and everything is working fine.目前,我正在使用 azure application insights 直接进行日志记录,如此链接中给出的使用最新版本的 Application Insight with .net core API一切正常。

But I need to use the serilog for logging now with the help of azure application insight.但是我现在需要借助 azure application insight 使用 serilog 进行日志记录。 Even I do some R&D about serilog ( https://github.com/serilog/serilog-sinks-applicationinsights ).甚至我也做了一些关于 serilog ( https://github.com/serilog/serilog-sinks-applicationinsights ) 的研发。 But didn't get any idea.但是没有任何想法。 Could you please suggest me that how can we achieve that with the.Net core 3.0您能否建议我如何使用.Net core 3.0 实现这一目标

Please follow the steps below:请按照以下步骤操作:

First, in stall the following packages:首先,安装以下软件包:

Microsoft.ApplicationInsights.AspNetCore, version 2.14.0 Microsoft.ApplicationInsights.AspNetCore,版本 2.14.0

Serilog.AspNetCore, version 3.2.0 Serilog.AspNetCore,版本 3.2.0

Serilog.Sinks.ApplicationInsights, version 3.1.0 Serilog.Sinks.ApplicationInsights,版本 3.1.0

Serilog.Settings.Configuration, version 3.1.0 Serilog.Settings.Configuration,版本 3.1.0

In Program.cs:在 Program.cs 中:

public class Program
{     
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
           .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
                    .ReadFrom.Configuration(hostingContext.Configuration)
                    .WriteTo.ApplicationInsights(new TelemetryConfiguration{ InstrumentationKey = "xxxxxxxxx" },TelemetryConverter.Traces)
             );                
}

In controller.cs :在 controller.cs 中

在此处输入图像描述

The test result :测试结果

在此处输入图像描述

Here is information how to setup app configuration for asp.net core, but it for v6.x以下是如何为 asp.net 核心设置应用程序配置的信息,但它适用于 v6.x

https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp.net-core?tabs.netcorenew%2.netcore6 https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp.net-core?tabs.netcorenew%2.netcore6

appsettings.json (example) appsettings.json(示例)

{
"Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
    "ConnectionString": "Copy connection string from Application Insights Resource Overview"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 net 6.0 应用程序:尝试登录 Azure 时启动时出现 Serilog 异常 Application Insights - net 6.0 application: Serilog exception on startup when trying to log to Azure Application Insights 在 .NET Core MVC 中使用 Serilog 使用 Azure 表存储进行日志记录 - Using Serilog for Logging with Azure Table Storage in .NET Core MVC 如何从 ASP.NET Core 应用程序将应用程序洞察数据发布到 REST API 而不是 Azure? - How to post application insights data to REST API instead of Azure from ASP.NET Core application? 将 Serilog 与 Caliburn Micro.Net Core 3.1 一起使用 - Using Serilog with Caliburn Micro .Net Core 3.1 如何将 Azure Application Insights for Asp.Net Core 5.0 配置为仅发送 4% 的请求和 100% 的异常? - How to configure Azure Application Insights for Asp.Net Core 5.0 to send only 4% of Requests and 100% of Exceptions? 具有Application Insights的asp.net core 2记录器 - asp.net core 2 Logger with Application Insights 在带有数据库的 ASP.NET Core 5.0 应用程序中使用 Serilog 实现日志记录 - Implement Logging Using Serilog In ASP.NET Core 5.0 Application With Database 通过Serilog在Azure上记录ASP.NET Core - Logging ASP.NET Core on Azure via Serilog 在Windows 10 IoT核心中使用Application Insights - Using Application Insights with Windows 10 IoT Core 使用调用堆栈进行Azure Application Insights调试 - Azure Application Insights Debugging using the Call Stack
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM