Chúng tôi đang sử dụng Owin trên Azure cho dịch vụ REST và phải báo cáo trực tiếp cho Thông tin chi tiết về ứng dụng. Chúng tôi muốn đăng nhập ngoại lệ và yêu cầu. Ngay bây giờ chúng tôi có điều này:Cách liên kết ngoại lệ với các yêu cầu trong Thông tin chi tiết về ứng dụng trên Azure?
using AppFunc = Func<IDictionary<string, object>, Task>;
public class InsightsReportMiddleware
{
readonly AppFunc next;
readonly TelemetryClient telemetryClient;
public InsightsReportMiddleware(AppFunc next, TelemetryClient telemetryClient)
{
if (next == null)
{
throw new ArgumentNullException("next");
}
this.telemetryClient = telemetryClient;
this.next = next;
}
public async Task Invoke(IDictionary<string, object> environment)
{
var sw = new Stopwatch();
sw.Start();
await next(environment);
sw.Stop();
var ctx = new OwinContext(environment);
var rt = new RequestTelemetry(
name: ctx.Request.Path.ToString(),
timestamp: DateTimeOffset.Now,
duration: sw.Elapsed,
responseCode: ctx.Response.StatusCode.ToString(),
success: 200 == ctx.Response.StatusCode
);
rt.Url = ctx.Request.Uri;
rt.HttpMethod = ctx.Request.Method;
telemetryClient.TrackRequest(rt);
}
}
public class InsightsExceptionLogger : ExceptionLogger
{
readonly TelemetryClient telemetryClient;
public InsightsExceptionLogger(TelemetryClient telemetryClient)
{
this.telemetryClient = telemetryClient;
}
public override Task LogAsync(ExceptionLoggerContext context, System.Threading.CancellationToken cancellationToken)
{
telemetryClient.TrackException(context.Exception);
return Task.FromResult<object>(null);
}
public override void Log(ExceptionLoggerContext context)
{
telemetryClient.TrackException(context.Exception);
}
}
Họ đã được đăng ký để ứng dụng của chúng tôi như sau:
static void ConfigureInsights(IAppBuilder app, HttpConfiguration config)
{
var rtClient = new TelemetryClient();
app.Use<InsightsReportMiddleware>(rtClient);
config.Services.Add(typeof (IExceptionLogger), new InsightsExceptionLogger(rtClient));
}
này hoạt động, trừ trường hợp ngoại lệ và yêu cầu are not connected. Cả hai đều được ghi lại, nhưng khi nhấp vào một yêu cầu thất bại, nó nói "Không tìm thấy ngoại lệ nào liên quan". Ngược lại, khi mở một thuộc tính ngoại lệ, chúng ta có thể đọc "Yêu cầu bị ảnh hưởng bởi ngoại lệ này: 0". Cách thích hợp để làm điều đó là gì?
"OperationIdTelemetryInitializer, đặt ITelemetry.Context.Operation.Id bằng RequestTelemetry.Id được trích xuất từ môi trường" bạn có ý tưởng về cách thực hiện điều đó trong OWIN không?Việc triển khai AspNet cuối cùng thực hiện 'httpContextAccessor.HttpContext.RequestServices.GetService()'. Làm thế nào nó có thể truy cập một bối cảnh địa phương, phạm vi từ một singleton toàn cầu? Làm thế nào điều này có thể hỗ trợ yêu cầu đồng bộ đồng thời? –
trethaller