如何在ASP.NET Core 2应用程序中唯一标识每个请求(用于日志记录) [英] How to unique identify each request in a ASP.NET Core 2 application (for logging)

查看:362
本文介绍了如何在ASP.NET Core 2应用程序中唯一标识每个请求(用于日志记录)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET Core 2应用程序中,我需要为每个请求提供唯一的标识符(例如Guid),以便可以在每个日志中包含该ID并了解每个请求的日志顺序.

In a ASP.NET Core 2 application, I need a unique identifier (e.g. Guid) for each request so I can include that id in each log and understand the sequence of logs of each request.

我自己编写它并不难,但是我想知道是否存在可以使用的内置功能或ASP.NET Core 2的实现方式.

This is not hard to write it myself, but I wonder if there is a builtin feature that I can use or a ASP.NET Core 2 way of achieving this.

推荐答案

您可以使用System.Diagnostics.Activity类(活动用户指南

You may use System.Diagnostics.Activity class (source). From Activity User Guide

Activity.Id用作根据 HTTP关联协议

例如, Microsoft/ApplicationInsights-aspnetcore 用于监控应用.

这篇关于如何在ASP.NET Core 2应用程序中唯一标识每个请求(用于日志记录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆