forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestMessageHandlerTracer.cs
More file actions
75 lines (65 loc) · 3.12 KB
/
RequestMessageHandlerTracer.cs
File metadata and controls
75 lines (65 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Properties;
namespace System.Web.Http.Tracing.Tracers
{
/// <summary>
/// Internal <see cref="DelegatingHandler"/> that executes before and after all of the installed message handlers.
/// The begin trace of this handler is the first trace for the request.
/// </summary>
internal class RequestMessageHandlerTracer : DelegatingHandler
{
private readonly ITraceWriter _traceWriter;
public RequestMessageHandlerTracer(ITraceWriter traceWriter)
{
Contract.Assert(traceWriter != null);
_traceWriter = traceWriter;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return _traceWriter.TraceBeginEndAsync<HttpResponseMessage>(
request,
TraceCategories.RequestCategory,
TraceLevel.Info,
String.Empty,
String.Empty,
beginTrace: (tr) =>
{
tr.Message = request.RequestUri == null ? SRResources.TraceNoneObjectMessage : request.RequestUri.ToString();
},
execute: () => base.SendAsync(request, cancellationToken),
endTrace: (tr, response) =>
{
MediaTypeHeaderValue contentType = response == null
? null
: response.Content == null
? null
: response.Content.Headers.ContentType;
long? contentLength = response == null
? null
: response.Content == null
? null
: response.Content.Headers.ContentLength;
if (response != null)
{
tr.Status = response.StatusCode;
}
tr.Message =
Error.Format(SRResources.TraceRequestCompleteMessage,
contentType == null
? SRResources.TraceNoneObjectMessage
: contentType.ToString(),
contentLength.HasValue
? contentLength.Value.ToString(CultureInfo.CurrentCulture)
: SRResources.TraceUnknownMessage);
},
errorTrace: null);
}
}
}