我试图了解如何从控制台应用程序记录到应用程序见解的简单日志记录。我创建了一个控制台应用程序,如下所示,将其作为 Web 作业托管在 Azure 中并运行。我可以从日志中看到它正在写入控制台,但我看不到在 Application Insights 中查找控制台日志的位置。我希望能够在出现错误时创建警报。
LoggingEngine类:[日志引擎类]
单击突出显示的日志图标并获取:[Web 作业日志]
但是当我进入监控|记录并运行他们都说没有结果的任何查询。
首先,我的代码正确吗? 其次,我正在使用 F1(免费)应用服务计划,这有关系吗? 第三,您能帮我查询 Application Insights 以查看控制台日志吗? 第四,我需要启用 Profiler 才能看到控制台写入吗?
任何帮助\建议将不胜感激, 谢谢,
我尝试了各种版本的代码,但似乎没有记录任何内容。我不知道我哪里错了。
我能够将跟踪记录到 Azure Web 作业中看到的 Application Insights。
网络作业日志:
Application Insights 交易搜索:
日志跟踪:
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();
}
}
}
}
b.AddApplicationInsightsWebJobs(o => o.InstrumentationKey = instrumentationKey);
我需要启用 Profiler 才能看到控制台写入吗?