Application Insights 未从控制台应用程序记录

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

我试图了解如何从控制台应用程序记录到应用程序见解的简单日志记录。我创建了一个控制台应用程序,如下所示,将其作为 Web 作业托管在 Azure 中并运行。我可以从日志中看到它正在写入控制台,但我看不到在 Application Insights 中查找控制台日志的位置。我希望能够在出现错误时创建警报。

LoggingEngine类:[日志引擎类]

程序.cs:

在 Azure 应用服务中 |网络工作:

单击突出显示的日志图标并获取:[Web 作业日志]

但是当我进入监控|记录并运行他们都说没有结果的任何查询。

首先,我的代码正确吗? 其次,我正在使用 F1(免费)应用服务计划,这有关系吗? 第三,您能帮我查询 Application Insights 以查看控制台日志吗? 第四,我需要启用 Profiler 才能看到控制台写入吗?

任何帮助\建议将不胜感激, 谢谢,

我尝试了各种版本的代码,但似乎没有记录任何内容。我不知道我哪里错了。

azure azure-application-insights azure-webjobs azure-webjobssdk
1个回答
0
投票

我能够将跟踪记录到 Azure Web 作业中看到的 Application Insights。

网络作业日志: enter image description here

Application Insights 交易搜索:

enter image description here

日志跟踪: enter image description here

  • 我已在
    Program.cs
    文件本身中配置了与 Application Insights 相关的代码。
  • 当您拥有自定义记录器时,您可以在
    LoggingEngine
    类中添加相同的内容或继续使用
    Program.cs
    文件。

我已参考此 MSDoc 在控制台应用程序中配置 ApplicationInsights。

  • 使用
    AddLogging
    部分代替
    ConfigureLogging

我的Program.cs文件:

using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace ConsoleApp2
{
    class Program
    {
        static async Task Main()
        {
            var builder = new HostBuilder();            
            builder.ConfigureLogging((context, b) =>
            {
                b.AddConsole();   
                //b.AddFilter("Microsoft.Azure.WebJobs.Hosting", LogLevel.None);
                //b.SetMinimumLevel(LogLevel.Information);
                string ConnString = "InstrumentationKey=****;IngestionEndpoint=https://****.in.applicationinsights.azure.com/;LiveEndpoint=https://****.livediagnostics.monitor.azure.com/;ApplicationId=****";
                if (!string.IsNullOrEmpty(ConnString))
                {
                    b.AddApplicationInsightsWebJobs(o => o.ConnectionString = ConnString);
                }
            });
            builder.ConfigureWebJobs(b =>
            {
                b.AddAzureStorageQueues();
            });
            var host = builder.Build();
            using (host)
            {
                var logger = host.Services.GetRequiredService<ILogger<Program>>();
                logger.LogInformation("Log Information from Progarm.cs..");
                logger.LogWarning("Warning Message");
                logger.LogDebug("Debug Message");
                logger.LogInformation("Application stopped.");

                await host.RunAsync();

            }
        }
    }
}
  • 您可以使用 Instrumentation key 或 Connection String。
 b.AddApplicationInsightsWebJobs(o => o.InstrumentationKey = instrumentationKey);
  • 在部署的 Azure 应用服务中,启用 Application Insights。

enter image description here

我需要启用 Profiler 才能看到控制台写入吗?

  • 对于基本日志和跟踪,无需启用探查器。如果您需要其他信息,则可以在 AppService => Application Insights 下启用它。

enter image description here

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