c# - 如何使用 Azure 函数注入 NLog
问题描述
我有以下代码来依赖注入 NLog 记录器:
// startup.cs
[assembly: FunctionsStartup(typeof(MyApp.FunctionApp.Startup))]
namespace MyApp.FunctionApp {
public class Startup : FunctionsStartup {
public override void Configure(IFunctionsHostBuilder builder) {
var nLogConfigPath = GetLogPath("nlog.config");
builder.Services.AddLogging(loggingBuilder =>
{
var nLogOptions = new NLogAspNetCoreOptions
{
RegisterHttpContextAccessor = true,
IgnoreEmptyEventId = true,
IncludeScopes = true,
ShutdownOnDispose = true
};
var logFactory = NLogBuilder.ConfigureNLog(nLogConfigPath);
logFactory.AutoShutdown = false;
var nLogConfig = logFactory.Configuration;
loggingBuilder.AddNLog(nLogConfig, nLogOptions);
});
}
}
}
// actual function code
public class ActualFunctionClass {
public ActualFunctionClass (ILogger<ActualFunctionClass> logger) {
logger.LogInformation("log stuff");
}
}
在 nlog.config 中,我有几个目标。如何确保logger
为 ActualFunctionClass 的参数配置了正确的目标?
这些是 nlog.config 的内容。作为记录,我想使用locations-dataload-file
目标登录。
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
throwExceptions="true"
keepVariablesOnReload="true">
<variable name="logDirectory" value="${basedir}/logs/locations-dataload" />
<variable name="commonLayout" value="${longdate}|${logger}|${uppercase:${level}}|${message}, ${all-event-properties:format=[key]=[value]:separator=, } ${exception}" />
<targets>
<target xsi:type="ApplicationInsightsTarget"
name="locations-dataload-ai"
layout="${commonLayout}" />
<target xsi:type="File"
name="locations-dataload-file"
fileName="${logDirectory}/locations-dataload-file-${shortdate}.log"
layout="${commonLayout}" />
<target xsi:type="Null" name="blackhole" />
</targets>
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="locations-dataload-ai" />
<!--All logs, including from Microsoft-->
<logger name="locations-dataload-local*" minlevel="Trace" writeTo="locations-dataload-ai,locations-dataload-file" />
<!-- Skip Microsoft logs and so log only own logs -->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
</rules>
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
<add assembly="MyApp.Logging" />
</extensions>
</nlog>
解决方案
我认为这是错误的:
<logger name="locations-dataload-local*" ...
记录器名称默认为 Namespace.ClassName
您可以通过以下方式进行测试:
将规则更改为name="*"
<logger name="*" minlevel="Trace" writeTo="locations-dataload-ai,locations-dataload-file" />
并添加到您的布局${logger}
中,例如
layout="${logger}|${commonLayout}"
名称过滤器将与记录器名称匹配${logger)
。因此,如果记录器名称是YourNamespace.ActualFunctionClass
thenlocations-dataload-local*
将不匹配。
推荐阅读
- r - 替换包含括号的字符串
- python - 如何在 Heroku 上的 selenium 机器人中添加和更新 excel 表?
- flutter - 如何将我的卡片轮播的硬编码数据更改为来自 api 响应的动态数据 -Flutter
- java - Spring freemarker 输入宏 - 使用插入视图作为更新视图
- python - 错误:即使将 tensorflow 导入为 tensorflow.compat.v1 后,模块“tensorflow”也没有属性“gfile”
- r - 水平移动Y轴标题ggplot2
- javascript - 在新选项卡中打开链接的 HTML 按钮
- django - Django 自定义用户模型和超级用户
- mongodb - ram配置中的mongodb缓存
- powerbi - 如何在 BI 中显示公司事件的上下文