c# - 如何将多个变量从方法传递到 NLOG.config?
问题描述
目前我在一个方法中有两个变量,
float processTime = processTime.ElapsedMilliseconds;
var rowsTotal = resultadoSQL.Rows.Count;
我想将这两个变量传递给 NLog.config,所以我可以像这样使用它们:
<target name="sql" xsi:type="File" fileName="C:\Users\Dev\Desktop\Framework\Logs\sql.txt"
layout="'Rows count:' (rowsTotal) 'Elapsed Time:' processTime"/>
显然布局中的语法是错误的,他的目的只是为了展示我想要实现的目标。.txt 文件如下所示:
行数:456 经过时间:87878
解决方案
也许将目标布局更改为${message}
并执行以下操作:
logger.Info($"'Rows count:' {rowsTotal} 'Elapsed Time:' {processTime}");
您还可以更高级并使用 NLog LogEvent 属性:
https://github.com/NLog/NLog/wiki/EventProperties-Layout-Renderer
如果使用 NLog 4.5,那么您可以使用结构化日志记录:
https://github.com/NLog/NLog/wiki/How-to-use-structured-logging
您还可以使用 NLog Fluent API:
https://github.com/NLog/NLog/wiki/Fluent-API
或者直接将属性写入NLog.LogEventInfo.Properties
推荐阅读
- c - 将多行写回文件,文件长度错误
- python - TensorFlow py_function 嵌套输出类型?
- python - 检查 pandas.core.series.Series 中项目出现的本机方法?
- node.js - 在 Sequelize 查找方法的条件中使用列别名时出错
- python - 不知道服务器 IP 时如何使用 Python SMB 连接?
- python - OpenCV:增强视频流源 .png 未按预期呈现透明度
- wso2 - 在 WSO2 流处理器中启动 Siddhi 应用程序时出错:javax/jms/JMSContext
- php - 如何将内容附加到php文件中的数组
- python-3.x - 使用字典中的两个对象创建一个嵌套对象
- r - R 语法中的替换/占位符变量