Logging Client IP address

Topics: Building and extending application blocks, Exception Handling Application Block, General discussion, Logging Application Block, Security Application Block
Dec 9, 2013 at 12:18 AM
Edited Dec 9, 2013 at 12:21 AM
I am using Enterprise Library 5.0 for logging and is working well. What I need now is, to log client IP address through reverse proxy. I am using the code below and can step into the code to see the IP addresses, but my issue now  is how to call the code in my application for logging using either logEntry.Write or any of the logging calls. Below is the code I am trying to use. Any help will be appreciated.
void Application_BeginRequest(object sender, EventArgs e)
    {
        // Get request.
        HttpRequest request = base.Request;
        if (base.Request == null)
        {
            // Let's first check for a proxy
            var ipAddresses = request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(',');
            }
        else if (request.UserHostAddress != null
                && request.UserHostAddress != string.Empty)
         
        {
            //Users IP Address.
            string address = request.UserHostAddress;
        }
        else
        {
          
            string IPAddresses = request.ServerVariables["REMOTE_ADDR"].ToString();
         
        }
    
    }

void Application_Error(object sender, EventArgs e) 
    {
         

         Exception ex = Server.GetLastError();
            if (ex is HttpUnhandledException && ex.InnerException != null)
            {
                Application_BeginRequest("ex", e);        
                ex = ex.InnerException;
            }
             
                
            if (ex != null)
            {

                Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(ex, "Policy");
                Server.ClearError();

                Response.Redirect("~/Utility/ErrorPage.htm");
            }
             
        }
Dec 9, 2013 at 2:53 AM
It looks like you are using the the Exception Handling Application Block. If you are using the Logging Exception Handler then the way to log the additional information would be to add the extra information to the Exception's IDictionary Data property.
if (ex != null)
{ 
    Application_BeginRequest("ex", e);        
    ex.InnerException;
}

ex.Data.Add("ClientIpAddress", ipAddress);

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(ex, "Policy");

Server.ClearError();
Response.Redirect("~/Utility/ErrorPage.htm");
All IDictionary key/value pairs will be added to the LogEntry's ExtendedProperties. You can then configure the Logging Application Block Formatter to log the applicable key/value. (e.g. In the XML formatter config add Client IP Address: {keyvalue(ClientIpAddress)}).

If you are using the logging block directly with Logger.Write(LogEntry) then you can add the IP Address directly to the LogEntry's ExtendedProperties.


~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Dec 9, 2013 at 3:16 AM
Hi Randy,
Thanks for your quick response. I am using more of the Logger.Write(LogEntry). I am now doing something like this and I hope you can let me know if I am doing the right thing here.

         
            LogWriter defaultWriter;
            defaultWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
            if (defaultWriter.IsLoggingEnabled())
            {
                Dictionary<string, object> exProperties = new Dictionary<string, object>();
                            
                exProperties.Add("IPaddress", Request.UserHostAddress);
                exProperties.Add("HTTP_X_FORWARDED_FOR", Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(','));
                exProperties.Add("REMOTE_ADDR", Request.ServerVariables["REMOTE_ADDR"].ToString());
                LogEntry entry1 = new LogEntry("LogEntry with category, priority, event ID, severity, and title.", "General", 8, 9006, TraceEventType.Error, "AppError", exProperties);
                defaultWriter.Write(entry1);
Dec 9, 2013 at 3:53 AM
That looks OK. Just make sure to configure the formatter to log the Extended Properties. For example to log all extended properties
Extended Properties: {dictionary({key} - {value}{newline})}
or {keyvalue(IPaddress)} to log the value of the IPAddress key.

On thing is that the object in the dictionary is going to have a ToString() performed on it so you would be better served leaving the HTTP_X_FORWARDED_FOR as a CSV instead of an array. If you want to break out each one then you could add each IPAddress to a separate key (HTTP_X_FORWARDED_FOR_1, HTTP_X_FORWARDED_FOR_2, etc.).

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Marked as answer by denkyira on 12/8/2013 at 7:54 PM
Dec 9, 2013 at 3:55 AM
Hi Randy,
Thanks a lot