为什么我看不到任何日志条目,但仍然可以在 Application Insights 中看到实时指标的变化?
我按照这个微软教程来实现应用程序我将用作网络作业的控制台应用程序的洞察日志记录。我的代码有几个问题。
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
namespace ConsoleApp5
{
class Program
{
public static void Main(string[] args)
{
IServiceCollection services = new ServiceCollection();
// Being a regular console app, there is no appsettings.json or configuration providers enabled by default.
// Hence instrumentation key and any changes to default logging level must be specified here.
services.AddLogging(loggingBuilder => loggingBuilder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("Category", LogLevel.Trace));
services.AddApplicationInsightsTelemetryWorkerService("xyz-xzy-xyz-xyz-xyz");
// Build ServiceProvider.
IServiceProvider serviceProvider = services.BuildServiceProvider();
// Obtain logger instance from DI.
ILogger<Program> logger = serviceProvider.GetRequiredService<ILogger<Program>>();
// Obtain TelemetryClient instance from DI, for additional manual tracking or to flush.
var telemetryClient = serviceProvider.GetRequiredService<TelemetryClient>();
int i = 0;
while (true) // This app runs indefinitely. replace with actual application termination logic.
{
// Replace with a name which makes sense for this operation.
using (telemetryClient.StartOperation<RequestTelemetry>("operation"))
{
telemetryClient.TrackTrace("VS This is tracktrace from telemetry" + i);//
logger.LogWarning("VS Logger.logWarning" + i);//
logger.LogInformation("VS logger.LogInformation" + i);
logger.LogTrace("VS logger.LogTrace" + i);
logger.LogError("VS logger.LogError" + i);//
logger.LogDebug("VS logger.LogDebug" + i);
logger.LogCritical("VS logger.LogCritical" + i);//
telemetryClient.Flush();
Task.Delay(5000).Wait();
}
i = i + 1;
}
}
}
}
但是当我在日志中运行traces
命令时,它没有显示任何内容,如下所示:
其次,为什么我没有看到 logger.LogInformation
、logger.LogTrace
和 logger.LogDebug
的任何日志?我已将日志级别设置为跟踪(LogLevel.Trace
)。
顺便说一句,我在最后使用 Task.Delay(5000).Wait();
以确保所有日志都刷新到我的 Application Insight。这对于生产环境来说也是正确的方法吗?
I followed this microsoft tutorial to implement Application Insight logging for my console application which I will be using as webjob. I have a couple of issues with the code.
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
namespace ConsoleApp5
{
class Program
{
public static void Main(string[] args)
{
IServiceCollection services = new ServiceCollection();
// Being a regular console app, there is no appsettings.json or configuration providers enabled by default.
// Hence instrumentation key and any changes to default logging level must be specified here.
services.AddLogging(loggingBuilder => loggingBuilder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("Category", LogLevel.Trace));
services.AddApplicationInsightsTelemetryWorkerService("xyz-xzy-xyz-xyz-xyz");
// Build ServiceProvider.
IServiceProvider serviceProvider = services.BuildServiceProvider();
// Obtain logger instance from DI.
ILogger<Program> logger = serviceProvider.GetRequiredService<ILogger<Program>>();
// Obtain TelemetryClient instance from DI, for additional manual tracking or to flush.
var telemetryClient = serviceProvider.GetRequiredService<TelemetryClient>();
int i = 0;
while (true) // This app runs indefinitely. replace with actual application termination logic.
{
// Replace with a name which makes sense for this operation.
using (telemetryClient.StartOperation<RequestTelemetry>("operation"))
{
telemetryClient.TrackTrace("VS This is tracktrace from telemetry" + i);//
logger.LogWarning("VS Logger.logWarning" + i);//
logger.LogInformation("VS logger.LogInformation" + i);
logger.LogTrace("VS logger.LogTrace" + i);
logger.LogError("VS logger.LogError" + i);//
logger.LogDebug("VS logger.LogDebug" + i);
logger.LogCritical("VS logger.LogCritical" + i);//
telemetryClient.Flush();
Task.Delay(5000).Wait();
}
i = i + 1;
}
}
}
}
Firstly, I am receiving logs in live metrics as shown in the screenshot below:
But when I am running the traces
command in logs it is not showing anything as shown below:
Secondly, Why I am not seeing any log of logger.LogInformation
, logger.LogTrace
and logger.LogDebug
? I have set my log level to trace(LogLevel.Trace
).
By the way, I am using Task.Delay(5000).Wait();
at the end to make sure all logs get flushed to my Application Insight. Is it the right approach for the Production environment as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是跟踪输出
Task.Delay(5000).Wait( );
生产环境中的命令甚至我的代码中使用的延迟命令。And here is the traces Output
Task.Delay(5000).Wait();
command in the production environment even delay command used in my code.