-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogActionFilter.cs
More file actions
131 lines (110 loc) · 5.1 KB
/
LogActionFilter.cs
File metadata and controls
131 lines (110 loc) · 5.1 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
namespace VELUX.VMS.Web.Filters
{
/// <summary>
/// LogAttribute handles all api request and responses. It adds logs with detailed info.
/// </summary>
public class LogActionFilter : ActionFilterAttribute
{
public const string RegexRemoveNewLineExpression = @"\r\n?|\n";
private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
{
var loggingInfo = ExtractLoggingInfoFromRequest(actionContext);
WriteLoggingInfo(loggingInfo);
return base.OnActionExecutingAsync(actionContext, cancellationToken);
}
public override Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
{
var loggingInfo = ExtractLoggingInfoFromResponse(actionExecutedContext);
WriteLoggingInfo(loggingInfo);
return base.OnActionExecutedAsync(actionExecutedContext, cancellationToken);
}
private ApiRequestLoggingInfo ExtractLoggingInfoFromRequest(HttpActionContext actionContext)
{
var info = new ApiRequestLoggingInfo
{
RequestTime = DateTime.Now.ToLongTimeString(),
CorrelationId = actionContext.Request.GetCorrelationId().ToString(),
HttpMethod = actionContext.Request.Method.Method,
UriAccessed = actionContext.Request.RequestUri.AbsoluteUri,
IPAddress = HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : "0.0.0.0"
};
info.RequestHeaders = actionContext.Request.Headers.ToList();
if (actionContext.Request.Content != null)
{
info.RequestArguments = JsonConvert.SerializeObject(actionContext.ActionArguments);
info.RequestFormat = actionContext.Request.Content.Headers.ContentType.MediaType;
var byteResponse = actionContext.Request.Content.ReadAsByteArrayAsync().Result;
using (var stream = new MemoryStream())
{
var context = (HttpContextBase)actionContext.Request.Properties["MS_HttpContext"];
context.Request.InputStream.Seek(0, SeekOrigin.Begin);
context.Request.InputStream.CopyTo(stream);
string requestBody = Encoding.UTF8.GetString(stream.ToArray());
info.BodyContent = Regex.Replace(requestBody, RegexRemoveNewLineExpression, string.Empty);
}
}
return info;
}
private ApiResponseLoggingInfo ExtractLoggingInfoFromResponse(HttpActionExecutedContext actionExecutedContext)
{
var info = new ApiResponseLoggingInfo
{
ResponseTime = DateTime.Now.ToLongTimeString(),
CorrelationId = actionExecutedContext.Request.GetCorrelationId().ToString()
};
if (actionExecutedContext.Response != null)
{
info.ResponseCode = ((int)actionExecutedContext.Response.StatusCode).ToString();
info.ResponseReasonPhrase = actionExecutedContext.Response.ReasonPhrase;
}
return info;
}
private void WriteLoggingInfo(ApiRequestLoggingInfo requestLoggingInfo)
{
Logger.Info(JsonConvert.SerializeObject(requestLoggingInfo, Formatting.Indented));
}
private void WriteLoggingInfo(ApiResponseLoggingInfo responseLoggingInfo)
{
Logger.Info(JsonConvert.SerializeObject(responseLoggingInfo, Formatting.Indented));
}
}
/// <summary>
/// Request logging info class.
/// </summary>
public sealed class ApiRequestLoggingInfo
{
public string CorrelationId { get; set; }
public string RequestTime { get; set; }
public string HttpMethod { get; internal set; }
public string UriAccessed { get; internal set; }
public string IPAddress { get; internal set; }
public string RequestFormat { get; internal set; }
public string RequestArguments { get; internal set; }
public string BodyContent { get; internal set; }
public List<KeyValuePair<string, IEnumerable<string>>> RequestHeaders { get; internal set; }
}
/// <summary>
/// Response logging info class.
/// </summary>
public sealed class ApiResponseLoggingInfo
{
public string CorrelationId { get; set; }
public string ResponseTime { get; set; }
public string ResponseCode { get; internal set; }
public string ResponseReasonPhrase { get; internal set; }
}
}