-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHttpLog.cs
More file actions
141 lines (118 loc) · 4.22 KB
/
HttpLog.cs
File metadata and controls
141 lines (118 loc) · 4.22 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
132
133
134
135
136
137
138
139
140
141
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
namespace uBeac.Web.Logging;
public sealed class HttpLog : Entity
{
public HttpLog()
{
var dateTime = DateTime.Now;
var assembly = Assembly.GetEntryAssembly();
var assemblyName = assembly.GetName();
var process = Process.GetCurrentProcess();
var thread = Thread.CurrentThread;
Time = dateTime;
AssemblyName = assemblyName.Name;
AssemblyVersion = assemblyName.Version?.ToString();
ProcessId = process.Id;
ProcessName = process.ProcessName;
ThreadId = thread.ManagedThreadId;
MemoryUsage = process.PrivateMemorySize64;
MachineName = Environment.MachineName;
EnvironmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
EnvironmentUserName = Environment.UserName;
}
public DateTime Time { get; set; }
public HttpRequestLog Request { get; set; }
public HttpResponseLog Response { get; set; }
public int StatusCode { get; set; }
public long Duration { get; set; }
public string AssemblyName { get; set; }
public string AssemblyVersion { get; set; }
public int ProcessId { get; set; }
public string ProcessName { get; set; }
public int ThreadId { get; set; }
public long MemoryUsage { get; set; }
public string MachineName { get; set; }
public string EnvironmentName { get; set; }
public string EnvironmentUserName { get; set; }
public IApplicationContext Context { get; set; }
public ExceptionModel Exception { get; set; }
}
public class HttpRequestLog
{
public HttpRequestLog() { }
public HttpRequestLog(HttpRequest request, string requestBody)
{
DisplayUrl = request.GetDisplayUrl();
Protocol = request.Protocol;
Method = request.Method;
Scheme = request.Scheme;
PathBase = request.PathBase;
Path = request.Path;
QueryString = request.QueryString.Value ?? string.Empty;
ContentType = request.ContentType ?? string.Empty;
ContentLength = request.ContentLength;
Headers = request.Headers.ToStringDictionary();
Body = requestBody;
}
public string DisplayUrl { get; set; }
public string Protocol { get; set; }
public string Method { get; set; }
public string Scheme { get; set; }
public string PathBase { get; set; }
public string Path { get; set; }
public string QueryString { get; set; }
public string ContentType { get; set; }
public long? ContentLength { get; set; }
public string Body { get; set; }
public IDictionary<string, string> Headers { get; set; }
}
public class HttpResponseLog
{
public HttpResponseLog() { }
public HttpResponseLog(HttpResponse response, string responseBody)
{
ContentType = response.ContentType;
ContentLength = response.ContentLength;
Body = responseBody;
Headers = response.Headers.ToStringDictionary();
}
public string ContentType { get; set; }
public long? ContentLength { get; set; }
public string Body { get; set; }
public IDictionary<string, string> Headers { get; set; }
}
public class ExceptionModel
{
public ExceptionModel() { }
public ExceptionModel(Exception exception)
{
Data = exception.Data;
HelpLink = exception.HelpLink;
HResult = exception.HResult;
Message = exception.Message;
Source = exception.Source;
StackTrace = exception.StackTrace;
}
public IDictionary Data { get; set; }
public string HelpLink { get; set; }
public int HResult { get; set; }
public string Message { get; set; }
public string Source { get; set; }
public string StackTrace { get; set; }
}
internal static class HeaderDictionaryExtensions
{
public static IDictionary<string, string> ToStringDictionary(this IHeaderDictionary dictionary)
{
var result = new Dictionary<string, string>();
foreach (var (key, value) in dictionary)
{
result.Add(key, value.ToString());
}
return result;
}
}