布局中的 nlog 子字符串
有没有办法将消息的子字符串发送到 nlog 布局? 像 ${${substring(0,200)}message}
这样的东西
<parameter name="@Summary" layout="${substring(0,200)}${message}"/>
如果能写出 message.substring(0, 200); 的等价物那就太酷了
<target type="Database" name="databaseLog"
ConnectionStringName="ApplicationConnectionString">
<commandText>
INSERT INTO [Log] ([Description] ,[Summary] ,[Level] )
VALUES (@Description, @Summary, @Level )
</commandText>
<parameter name="@Description" layout="${message}"/>
<parameter name="@Summary" layout="{{SUBSTRING-OF-MESSAGE-HERE}}"/>
<parameter name="@Level" layout="${level}"/>
</target>
is there a way to get a substring of the message to the nlog layout?
something like ${${substring(0,200)}message}
<parameter name="@Summary" layout="${substring(0,200)}${message}"/>
It would be cool if that would write the equlivent of message.substring(0, 200);
<target type="Database" name="databaseLog"
ConnectionStringName="ApplicationConnectionString">
<commandText>
INSERT INTO [Log] ([Description] ,[Summary] ,[Level] )
VALUES (@Description, @Summary, @Level )
</commandText>
<parameter name="@Description" layout="${message}"/>
<parameter name="@Summary" layout="{{SUBSTRING-OF-MESSAGE-HERE}}"/>
<parameter name="@Level" layout="${level}"/>
</target>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 ${pad} 布局渲染器:
正填充值导致左侧填充,负值导致右侧填充到所需的宽度。 文档
或者尝试使用 T-SQL SUBSTR:
You may use ${pad} layout renderer:
Positive padding values cause left padding, negative values cause right padding to the desired width. Documentation
Or try to use T-SQL SUBSTR:
NLog 4.6.3 现在可以执行子字符串逻辑:
https://github.com /NLog/NLog/wiki/Substring-layout-renderer
并截断:
NLog 4.6.3 can now perform substring-logic:
https://github.com/NLog/NLog/wiki/Substring-layout-renderer
And truncate:
试试这个。我遇到了同样的问题,最终写下了这篇文章。
它是一个包装渲染器。通过在 nlog 配置文件中添加“扩展”标签来使用,如下所示,
这将为您提供从日志消息开头算起的 1000 个字符和从结尾算起的 200 个字符,并在中间显示一条警告,通知字符串不完整。
您可以通过设置“warnMsg”值来定制警告消息,或者通过将“showWarnMsg”设置为“false”来完全关闭它。
'headLength' 和 'tailLength' 可以单独使用来给出从字符串的开头或结尾开始的子字符串。如果没有给出任何值或者字符串比指定值的总和短,则字符串将按原样返回。
Try this. I had the same problem and ended up writing this.
It is a wrapper renderer. Use by adding a 'extensions' tag to your nlog config file like
This will give you 1000 chars from the beginning and 200 chars from the end of the log message with a warning in the middle that notifies the string is not complete.
You can tailor the warning message by setting the 'warnMsg' value or turn it completely off by setting 'showWarnMsg' to 'false'.
'headLength' and 'tailLength' can be used alone to give a substring from the begining or the end of the string. If neither value is given or if the string is shorter than the sum of the value(s) specified, the string will be returned as is.