使用 Microsoft.Extensions.Logging 进行日志记录时如何返回日志的 ID
在我的 .NET 6 Web 应用程序中,我使用以下基础结构来记录各种事件和异常:
- Microsoft.Extensions.Logging 作为日志记录 API
- Serilog 作为日志记录框架
- MS SQL 数据库作为日志接收器(即日志所在的目标)书面)。
在 Web 应用程序的 API 层中,有一个中间件可以捕获所有异常,记录它们并将异常消息作为 JSON 字符串返回给客户端。
我还想将创建的日志条目的 ID 与异常消息一起返回给客户端。不幸的是,Microsoft.Extensions.Logging 的LoggerExtension
类的方法(例如LogError()
)不返回任何值。有没有办法获取创建的数据库记录的ID?
In my .NET 6 web application I'm using the following infrastructure to log various events and exceptions:
- Microsoft.Extensions.Logging as logging API
- Serilog as logging framework
- MS SQL database as loging sink (i.e. the target where the logs are going to be written).
In the API layer of the web application there's a middleware that catches all exceptions, logs them and returns the exception message as JSON string to the client.
Together with the exception message I'd like to return also the ID of the created log-entry to the client. Unfortunately, the methods of Microsoft.Extensions.Logging's LoggerExtension
class (e.g. LogError()
) don't return any value. Is there any way to obtain the ID of the created database record?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论