代码之家  ›  专栏  ›  技术社区  ›  RPM1984

如何在AppInsights遥测初始值设定项中访问OWIN请求?

  •  1
  • RPM1984  · 技术社区  · 7 年前

    主要是,我想提取 POST 身体

    this other answer

    但是,答案表明使用 HttpContext.Current.Request.InputStream .

    我不能这么做。。。就像我在OWIN环境中一样。

    谢谢

    2 回复  |  直到 7 年前
        1
  •  2
  •   baywet    7 年前



    在这个过程中,他们删除了大多数静态上下文变量(如httpContext.Current),等价的信息将由OWIN基础设施作为OwinContext传递给您的中间件。
    也就是说,如果您使用Application Insights,您不必从头开始编写中间件,这是一个非常好的解决方案 nuget package 帮助您更快到达目的地。 see related github issue

        2
  •  0
  •   Simone    7 年前

    我只是想延长 baywet 的答案。

    如果可以的话,我建议使用现有的 nuget package 使用OWIN扩展应用程序洞察力。在这种情况下,你至少需要 .NET Framework 4.6.1

    否则,您可以使用“我的”解决方案。我刚刚混合了这两个答案:

    我需要的是记录异常和请求。这就是我所做的。。(我在这里写了一些基本的东西,让你写出一个有效的解决方案)。

    TrackerException 和a TrackerRequest Proxy Pattern . 通过这种方式,我显然不必关心我决定跟踪的内容。我只是让你们看看一个跟踪器的一个更简单的实现:

    public class TrackerException : ITracker
    {
        private readonly TelemetryClient _telemetryClient;
        public TrackerException(TelemetryClient telemetryClient)
        {
            this._telemetryClient = telemetryClient;
        }
    
        public void Track(ITelemetryObject telemetry)
        {
            if (telemetry is TelemetryExceptionObject)
            {
                Exception ex = ((TelemetryExceptionObject)telemetry).Exception;
                this._telemetryClient.TrackException(ex);
            }
        }
    }
    

    我在自定义Owin中间件中使用跟踪器:

    public class ApplicationInsightsMiddleware : OwinMiddleware
    {
        private TrackingOptions _trackingOptions;
    
        public ApplicationInsightsMiddleware(OwinMiddleware next) 
            : base(next)
        { }
    
        public ApplicationInsightsMiddleware(OwinMiddleware next, TrackingOptions trackingOptions) 
            : base(next)
        {
            this._trackingOptions = trackingOptions;
        }
    
        public override async Task Invoke(IOwinContext context)
        {
            var client = new TelemetryClient();
            this._trackingOptions.TelemetryClient = client;
    
            // Start Time Tracking
            var sw = new Stopwatch();
            var startTime = DateTimeOffset.Now;
            sw.Start();
    
            this._trackingOptions.TrackRequest.ReadRequestBody(context.Request);
    
            try
            {
                await Next.Invoke(context); 
            }
            catch (Exception ex)
            {
                ITelemetryObject exception = new TelemetryExceptionObject(ex);
                this._trackingOptions.TrackException.Track(exception);
    
                throw ex;
            }
    
            RequestTelemetry requestTelemetry = this._trackingOptions.TrackRequest.CreateTelemetryRequest(context.Request, context.Response, startTime, sw.Elapsed);
            ITelemetryObject request = new TelemetryRequestObject(requestTelemetry);
            this._trackingOptions.TrackRequest.Track(request);
    
            sw.Stop();
        }
    }
    

    TrackingOptions 是一个只启用或不启用跟踪器的类。。。以下是其实施:

    public class TrackingOptions
    {
        //Here I initialize all my trackers
    
        private ProxyTrackerException _trackException;
        internal ProxyTrackerException TrackException { get { return _trackException; } }
    
        private TelemetryClient _telemetryClient;
        internal TelemetryClient TelemetryClient
        {
            get { return _telemetryClient; }
            set { _telemetryClient = value; }
        }
    
        public TrackingOptions(bool enableTrackingRequest, bool enableTrackingException)
        {
            this.InitializeExceptionTracker(enableTrackingException);
            //...
        }
    
        private void InitializeExceptionTracker(bool enableTrackingException)
        {
            this._trackException = new ProxyTrackerException(enableTrackingException, this._telemetryClient);
        }
    }
    

    最后,我为Owin编写了一个扩展方法

    public static void UseApplicationInsights(this IAppBuilder app, TrackingOptions options)
    {
        app.Use(typeof(ApplicationInsightsMiddleware), options);
    }
    

    app.UseApplicationInsights(
        new TrackingOptions(enableTrackingRequest: true, enableTrackingException: true)
    );
    

    然而,这对我来说是一个暂时的解决方案。。。我会尽快更新我的框架并安装现有的nuget包。