我有一个简单的Azure函数,我希望能够在流日志窗口和Application Insights中监视日志输出。
到目前为止,我能够在Application Insights中看到NLog输出,但在流窗口中却看不到。 Microsoft ILogger输出将同时出现在两者中。
这是我到目前为止所做的:
最后,我修改了 host.json ,将默认日志记录级别更改为“ Trace”。为了使流窗口显示Microsoft ILogger的跟踪级别的输出,这是必需的,我以为这可能是Nlog输出都没有显示的原因...
{
"version": "2.0",
"logging": {
"logLevel": {
"default": "Trace"
}
}
}
到目前为止,结果是:
这是最终的功能代码...
public static class Function1
{
[FunctionName("LogTest")]
public static void Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
{
// Set up NLOG targets and logger
var config = new LoggingConfiguration();
//send logging to application insights
config.LoggingRules.Add(
new LoggingRule("*", LogLevel.Trace,
new ApplicationInsightsTarget()));
//also try to log to Trace output
//'RawWrite' is used to try and force output at all log-levels
config.LoggingRules.Add(
new LoggingRule("*", LogLevel.Trace,
new TraceTarget {RawWrite = true}));
LogManager.Configuration = config;
var nlog = LogManager.GetLogger("Example");
//log using native
log.LogInformation($"log:Info"); //appears in live-stream, app-insights
log.LogError("log:Error"); //appears in live-stream, app-insights
log.LogTrace("log:Trace"); //appears in live-stream, app-insights (after modifying host.json)
//log using nlog
nlog.Info("nlog:info"); //appears in ........... app-insights
nlog.Error("nlog:error"); //appears in ........... app-insights
nlog.Trace("nlog:trace"); //appears in ........... app-insights
//say goodbye
log.LogInformation("log:ending");
}
}
在此先感谢您提出任何建议-毫无疑问,我缺少一些简单的步骤。
答案 0 :(得分:4)
感谢Rolf Kristensen提供的the link
看来解决方案是使用MicrosoftILoggerTarget
而不是TraceTarget
配置新规则。
因此完整的代码是
var config = new LoggingConfiguration();
//ensure that log output is seen in the Streamed Log window
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace,
new MicrosoftILoggerTarget(azureLog)));
//ensure that log output is sent to Application Insights
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace,
new ApplicationInsightsTarget()));
LogManager.Configuration = config;
var nlog = LogManager.GetLogger("Example");
nlog.Info("output from nlog");
其中azureLog是提供给该函数的Run方法的ILogger
。
MicrosoftILoggerTarget
可在NLog.Extensions.Logging nuget包中找到。