-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextLoggerModule.cs
More file actions
90 lines (71 loc) · 2.79 KB
/
TextLoggerModule.cs
File metadata and controls
90 lines (71 loc) · 2.79 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
using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
namespace Bharat.ASPNetRequestLogger
{
public class TextLoggerModule : IHttpModule
{
/// <summary>
/// You will need to configure this module in the Web.config file of your
/// web and register it with IIS before being able to use it. For more information
/// see the following link: http://go.microsoft.com/?linkid=8101007
/// </summary>
#region IHttpModule Members
public void Dispose()
{
//clean-up code here.
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(OnBeginRequest);
}
#endregion
public void OnBeginRequest(object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
try
{
var logFilter = ConfigurationManager.AppSettings["asp_url_filter"] ?? Environment.GetEnvironmentVariable("asp_url_filter") ?? "";
if (!string.IsNullOrWhiteSpace(logFilter))
{
var regex = new Regex(logFilter, RegexOptions.IgnoreCase);
if (!regex.IsMatch(context.Request.Url.ToString()))
{
return;
}
}
var tempFolder = context.Server.MapPath("/ASP_LOGS/");
Directory.CreateDirectory(tempFolder);
var requestLogBuilder = new StringBuilder();
requestLogBuilder.AppendLine("URL:");
requestLogBuilder.AppendLine(context.Request.Url.ToString());
requestLogBuilder.AppendLine();
requestLogBuilder.AppendLine("Headers:");
foreach (string headerKey in context.Request.Headers)
{
requestLogBuilder.AppendLine($"{headerKey}: {context.Request.Headers[headerKey]}");
}
requestLogBuilder.AppendLine();
requestLogBuilder.AppendLine("Body:");
var bodyReader = new StreamReader(context.Request.InputStream);
var bodyStr = bodyReader.ReadToEnd();
requestLogBuilder.AppendLine(bodyStr);
var logFileName = tempFolder + Guid.NewGuid().ToString("n") + ".txt";
File.WriteAllText(logFileName, requestLogBuilder.ToString());
}
catch
{
}
finally
{
context.Request.InputStream.Position = 0;
}
}
}
}