NLog - 将 ${message} 保存在多个数据库列中
我想将记录的消息 ${message}
保存在数据库的多个列中,如以下示例所示:
我的记录器消息将遵循路径 User|Action
>,例如:
logger.Info("John Doe|Logged in application"}
logger.Info("Mike Doe|Deleted a file"}
现在我想将User
保存在数据库的一列中,例如logsTable.user
,并将Action
保存在另一列中,例如logsTable.action
。
有没有办法用正则表达式或其他一些规则解析 ${message}
(根据特定字符分隔消息,在我的示例中它是“|
” )保存到参数中(在我的例子中,我希望消息的第一部分放入 @user
参数中,第二部分放入 @action
参数中)?
I'd like to save the logged message ${message}
in several columns in a database, as shown on the following example:
My logger messages will follow the path User|Action
, e.g:
logger.Info("John Doe|Logged in application"}
logger.Info("Mike Doe|Deleted a file"}
Now I'd like to save User
in a column in my database, e.g logsTable.user
, and Action
in another column, e.g logsTable.action
.
Is there any way to parse the ${message}
with a regex or some other rules (to separate messages according to a specific character, in my example it's a "|
") to save into parameters (in my case, i'd like the first part of the message to go in @user
parameter and the second part in @action
parameter)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 NLog 文档,将自己的属性添加到日志事件应该不会太复杂。然后,您可以在正确的 NLog 接口上执行扩展方法并编写类似以下内容(未编译):
现在您应该能够引用
${event-context:item=User}
According to NLog documentation it shouldn't be too complicated to add your own properties to a log event. Then you could do an extension method on the correct NLog interface and write something like this (uncompiled):
Now you should be able to refer to
${event-context:item=User}