11using eCommerce . Helpers . Logging ;
22using System ;
3- using System . Collections . Generic ;
4- using System . Linq ;
5- using System . Net ;
6- using System . Net . Http ;
73using System . Security . Cryptography ;
84using System . Text ;
95using System . Web ;
@@ -16,42 +12,29 @@ public class WebRequestCorrelationIdentifier : IRequestCorrelationIdentifier
1612
1713 public WebRequestCorrelationIdentifier ( )
1814 {
19- string ipAddress = getIPAddress ( ) ;
20- string userName = HttpContext . Current . Request . Params [ "LOGON_USER" ] ;
21- string userAgent = HttpContext . Current . Request . UserAgent . ToString ( ) ;
22- string time = DateTime . Now . ToString ( "dd/MM/yyyy HH:mm:ss" ) ;
23-
24- string rawId = time + "|" + ipAddress + "|" + userName + "|" + userAgent ;
25-
26- this . CorrelationID = md5Hash ( rawId ) ;
27- }
28-
29- private static string getIPAddress ( )
30- {
31- string ip = HttpContext . Current . Request . ServerVariables [ "HTTP_X_FORWARDED_FOR" ] ;
32-
33- if ( string . IsNullOrEmpty ( ip ) )
34- ip = HttpContext . Current . Request . ServerVariables [ "REMOTE_ADDR" ] ;
35- else
36- ip = ip . Split ( ',' ) [ 0 ] ;
37-
38- return ip ;
39- }
40-
41- private static string md5Hash ( string input )
42- {
43- MD5 md5 = System . Security . Cryptography . MD5 . Create ( ) ;
44-
45- byte [ ] inputBytes = System . Text . Encoding . ASCII . GetBytes ( input ) ;
46- byte [ ] hash = md5 . ComputeHash ( inputBytes ) ;
47-
48- StringBuilder sb = new StringBuilder ( ) ;
49- for ( int i = 0 ; i < hash . Length ; i ++ )
15+ /* #Customise your correlation ID here
16+ * More request identification varibles 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 varibles 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 ( ) )
5031 {
51- sb . Append ( hash [ i ] . ToString ( "X2" ) ) ;
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" ) ) ;
5235 }
5336
54- return sb . ToString ( ) ;
37+ this . CorrelationID = hashBuilder . ToString ( ) ;
5538 }
5639 }
5740}
0 commit comments