Skip to content

Commit 4d94f14

Browse files
committed
Updated Correlation Identifier
1 parent 007ad2a commit 4d94f14

File tree

3 files changed

+43
-41
lines changed

3 files changed

+43
-41
lines changed

eCommerce.WebService/App_Start/Installers/DistributedInterfaceLayerInstall.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKern
1717
.BasedOn<IHttpController>()
1818
.Configure(c => c.LifestyleTransient()));
1919

20-
container.Register(Component.For<IRequestCorrelationIdentifier>().ImplementedBy<WebRequestCorrelationIdentifier>().LifeStyle.PerWebRequest);
20+
container.Register(Component.For<IRequestCorrelationIdentifier>().ImplementedBy<W3CWebRequestCorrelationIdentifier>().LifeStyle.PerWebRequest);
2121
}
2222
}
2323
}

eCommerce.WebService/App_Start/WebRequestCorrelationIdentifier.cs renamed to eCommerce.WebService/App_Start/W3CWebRequestCorrelationIdentifier.cs

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,42 @@
1-
using eCommerce.Helpers.Logging;
2-
using System;
3-
using System.Security.Cryptography;
4-
using System.Text;
5-
using System.Web;
6-
7-
namespace eCommerce.WebService.App_Start
8-
{
9-
public class WebRequestCorrelationIdentifier : IRequestCorrelationIdentifier
10-
{
11-
public string CorrelationID { get; private set; }
12-
13-
public WebRequestCorrelationIdentifier()
14-
{
15-
/* #Customise your correlation ID here
16-
* More request identification variables you add easier
17-
* it's going to be to find the relevant W3C request when you hash it
18-
*
19-
* Below is just an example of variables and hash algorithm that you can use:
20-
*/
21-
string rawCorrelationID = string.Join("_",
22-
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"],
23-
HttpContext.Current.Request.Params["LOGON_USER"],
24-
HttpContext.Current.Request.UserAgent.ToString(),
25-
HttpContext.Current.Request.Path,
26-
DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss")
27-
);
28-
29-
StringBuilder hashBuilder = new StringBuilder();
30-
using (MD5 md5 = MD5.Create())
31-
{
32-
byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(rawCorrelationID));
33-
for (int i = 0; i < hash.Length; i++)
34-
hashBuilder.Append(hash[i].ToString("X2"));
35-
}
36-
37-
this.CorrelationID = hashBuilder.ToString();
38-
}
39-
}
1+
using eCommerce.Helpers.Logging;
2+
using System;
3+
using System.Security.Cryptography;
4+
using System.Text;
5+
using System.Web;
6+
7+
namespace eCommerce.WebService.App_Start
8+
{
9+
public class W3CWebRequestCorrelationIdentifier : IRequestCorrelationIdentifier
10+
{
11+
public string CorrelationID { get; private set; }
12+
13+
public W3CWebRequestCorrelationIdentifier()
14+
{
15+
/* #Customise your correlation ID here
16+
* More request identification variables you add easier
17+
* it's going to be to find the relevant W3C request when you hash it
18+
*
19+
* Below is just an example of variables and hash algorithm that you can use:
20+
*/
21+
string rawCorrelationID = string.Join("_",
22+
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"],
23+
HttpContext.Current.Request.Params["LOGON_USER"],
24+
HttpContext.Current.Request.UserAgent != null ?
25+
HttpContext.Current.Request.UserAgent.ToString().Replace(" ", "+") : "-",
26+
HttpContext.Current.Request.Path,
27+
HttpContext.Current.Request.QueryString.ToString() ?? "-",
28+
new DateTime(HttpContext.Current.Timestamp.Ticks).ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss")
29+
);
30+
31+
StringBuilder hashBuilder = new StringBuilder();
32+
using (MD5 md5 = MD5.Create())
33+
{
34+
byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(rawCorrelationID));
35+
for (int i = 0; i < hash.Length; i++)
36+
hashBuilder.Append(hash[i].ToString("X2"));
37+
}
38+
39+
this.CorrelationID = hashBuilder.ToString();
40+
}
41+
}
4042
}

eCommerce.WebService/eCommerce.WebService.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
<ItemGroup>
149149
<Compile Include="App_Start\BootstrapConfig.cs" />
150150
<Compile Include="App_Start\FilterConfig.cs" />
151-
<Compile Include="App_Start\WebRequestCorrelationIdentifier.cs" />
151+
<Compile Include="App_Start\W3CWebRequestCorrelationIdentifier.cs" />
152152
<Compile Include="App_Start\Installers\ApplicationLayerInstall.cs" />
153153
<Compile Include="App_Start\Installers\DistributedInterfaceLayerInstall.cs" />
154154
<Compile Include="App_Start\Installers\DomainModelLayerInstall.cs" />

0 commit comments

Comments
 (0)