- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到一个问题,我需要使用 Nlog 将日志写入 Azure 表,但连接字符串可能会根据环境(即 Dev/UAT 等)而变化,因此我需要从另一个配置文件中获取它。我的 Nlog“目标”部分当前如下所示:
<targets>
<target xsi:type="File" name="allfile" fileName="${var:configDir}\nlog-all.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<target xsi:type="AzureTableStorage"
connectionString="${var:myNLogConnectionString}"
name="NLogAzureTable"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}"
tableName="MyTestLogs"
logTimeStampFormat="O" />
</targets>
我的 Api 如下所示:
ILoggerFactory logger = new LoggerFactory().AddNLog();
var nlogConfigSection = config.Settings.Sections["MyService_NlogSettings"];
LogManager.LoadConfiguration("nlog.config");
LogManager.Configuration = new XmlLoggingConfiguration("nlog.config");
LogManager.Configuration.Variables["configDir"] = nlogConfigSection.Parameters["FileLocation"].Value;
LogManager.Configuration.Variables["myNLogConnectionString"] = nlogConfigSection.Parameters["environmentNLogConnectionString"].Value;
从调试中我可以看到 config.settings 值都已按要求检索,甚至配置中的变量都已正确填充。我发现,如果我在本地写入“allfile”文本文件,它会设法检索并填充“configDir”,因为那是文本文件出现的位置!但是,如果我切换到使用 Azure,我可以看到如前所述,该变量正在配置中设置,但是当我查看 Nlog 内部日志文件时,我可以看到它认为连接字符串为空。
有什么明显的地方表明我做错了吗?!?我见过类似问题的解决方案,但它们似乎总是涉及做我已经做过的事情,但我没有得到任何快乐!
最佳答案
尝试这样做:
ILoggerFactory logger = new LoggerFactory().AddNLog();
var nlogConfigSection = config.Settings.Sections["MyService_NlogSettings"];
// Configure global settings before loading NLog.config
NLog.GlobalDiagnosticsContext.Set("configDir", nlogConfigSection.Parameters["FileLocation"].Value);
NLog.GlobalDiagnosticsContext.Set("myNLogConnectionString", nlogConfigSection.Parameters["environmentNLogConnectionString"].Value);
NLog.LogManager.Configuration = new XmlLoggingConfiguration("nlog.config");
使用以下 NLog.config,将 ${var
替换为 ${gdc
:
<targets>
<target xsi:type="File" name="allfile" fileName="${var:configDir}\nlog-all.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<target xsi:type="AzureTableStorage"
connectionString="${gdc:myNLogConnectionString}"
name="NLogAzureTable"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}"
tableName="MyTestLogs"
logTimeStampFormat="O" />
</targets>
关于c# - 如何使用设置变量为 NLog 配置 Azure 表连接字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54556539/
我想更改 nlog 布局,以便显示两个条目之间的时间。这样的布局可能吗? 这是我现在的输出,如果在时间旁边有自之前进入以来经过的秒数,那就太好了。 2012-05-20 19:18:41.09
使用 NLog v4.4.12,我的 App.config 中有这个 这是我的 Variables.xml 文件内容 但是我在启动我的应
我有一个名为 的自定义布局渲染器工作 .它提供了几个项目,它们在我们的 app.config 中是这样使用的: 当
NLog 是否可以发出多行消息,以便每行都根据当前布局进行格式化?例如。 2015-12-17 11:37:38.0845 | 64 | INFO | ------------------------
我刚开始玩 nlog,注意到 nlog.xml 文件与 nlog.dll 一起被带到了应用程序的输出文件夹中。我不太清楚该文件的用途以及我是否应该将它作为我的应用程序安装的一部分或可以安全地删除它。有
是否可以/容易使用 Rhino Mocks 或类似方法模拟 NLog 日志方法? 最佳答案 使用 Nuget:install-package NLog.Interface 然后:ILogger log
目标: 当我打电话时Logger.Error("some message", e) ,其中 e是一些异常对象,它只记录消息,不记录异常信息。我需要它来输出异常消息和堆栈跟踪。任何想法我做
在生产环境中更改 nLog 日志记录级别的最佳做法是什么。我可以更改它的唯一方法是修改 NLog.config 文件,然后回收应用程序池。应该这样吗? 谢谢, 克里斯 最佳答案 如果你想改变日志配置而
我已经创建了一个通用实现 ILogger 并在这个包装器中实现了 NLog 实现方法。我的问题是我在我的解决方案中为整个 namspace 启用了错误严重性,但我还希望对“Employee”命名空间下
我在 NLog.config 中配置了两条规则: 我正在尝试使用以下代码写入第一个: LogEventInfo eventInfo = new LogEventInfo(); eventInfo.
我正在尝试将一堆应用程序使用的现有日志记录库转换为使用 NLog。现有的日志记录代码没有间接性,日志记录调用直接从调用者到 Web 服务调用。现有的日志记录代码是作为静态单例实现的。我需要以这样一种方
我有一个泛型类: public class GenericClass { private static readonly Logger Logger = LogManager.GetCurre
在我看来,NLog 中的内置日期格式没有正确包含时区。我们需要用尾随 Z 记录 UTC 时间,以便 Splunk 知道本地时间是什么,例如: {日期:universalTime=true:format
我们提供了一个用于日志记录的框架程序集,它在内部使用 nlog。我们还提供了一个嵌入式 nlog 配置作为我们程序集中的资源,并在启动时读取它(包装器中的静态构造函数,它使用 XmlLoggingCo
我这样编码: private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); public MyClass()
现在我这样做 LogManager.DisableLogging(); 但这需要再次部署我的代码,如何从 nlog 配置文件中禁用登录。 最佳答案 关于nlog - 从 Nlog.config 发
我想弄清楚如何阻止 NLog 替换我正在记录的字符串中的换行符。我希望输出包含所有换行符,而不是将整个输出放在一行中。 有人可以帮忙吗? 配置: 代码
作为从 NLog 切换到 Serilog 的一个步骤,我想重定向 NLog 的 LogManager.GetLogger(name) 的标准调用的标准接线,以桥接任何记录到 NLog 的代码,以便立即
为此,我需要一份详细/分步指南。我已经阅读了简要指南 ( example here ) 并下载了示例代码,但我仍然无法弄清楚如何使用 Nlog 登录到 CloudWatch。 当我在 NLog.con
我将 NLog 错误电子邮件文本的内容放入一个文件中,并使用 FileContents 渲染器进行渲染。如果布局渲染器产生空字符串,我想做的是隐藏 html 输出的某些部分 Request ticke
我是一名优秀的程序员,十分优秀!