5

I am developing a Web Application in ASP.net, which requires login name and password.

I want to log IP Address and Machine Name of client, who are accessing this Web Application.

I am using log4net for logging.

I have tried this piece of code, but I am getting Server Machine HostName in log after deploying this web application using IIS-7 instead of Client Machine Name.

Login Page Page_Load Method:

protected void Page_Load(object sender, EventArgs e)
{
    log4net.GlobalContext.Properties["Hostname"] = Dns.GetHostName();
}

Web.Config Changes:

 <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %property{Hostname} [%thread] %-5level %logger - %message%newline" />
 </layout>

Its really huge project, So Please suggest me the way which requires minimum changes in Code to log the IP Address and Machine Name of Client.

TylerH
  • 20,799
  • 66
  • 75
  • 101
user3264676
  • 253
  • 5
  • 8
  • 20
  • Duplicate of [Get client's IP address and computer name?](https://stackoverflow.com/questions/7462188/get-clients-ip-address-and-computer-name) – TylerH Sep 02 '20 at 20:33

4 Answers4

17

Did you try using following?

Request.UserHostAddress

Request.UserHostName

Refer to following answer posted on a different thread

Get client's IP address and computer name?

Community
  • 1
  • 1
Sam
  • 2,917
  • 1
  • 15
  • 28
5

Try For Client Computer name..

string[] computer_name = System.Net.Dns.GetHostEntry(Request.ServerVariables["remote_addr"]).HostName.Split(new Char[] { '.' });
  String ecname = System.Environment.MachineName;
            txtComputerName.Text = computer_name[0].ToString();

For Client IP address..

Request.ServerVariables["REMOTE_ADDR"]

Check this also... https://stackoverflow.com/a/19286308/3156647

Community
  • 1
  • 1
tarzanbappa
  • 4,930
  • 22
  • 75
  • 117
0
Dns.GetHostEntry(log.IPAddress).HostName

For some reason the other answers didn't work on my shared hosting server. Go figure...

Alexandru
  • 12,264
  • 17
  • 113
  • 208
0

For Client IP:

System.Web.HttpContext.Current.Request.ServerVariables["**REMOTE_ADDR**"];

For Client Hostname:

System.Net.Dns.GetHostEntry(Request.ServerVariables["**REMOTE_HOST**"]).HostName;
Avinash Dalvi
  • 8,551
  • 7
  • 27
  • 53
  • 1
    Ensure that the server on which this application is hosted must have correct entry for the DNS Servers in TCP/IP configuration. – Satish Asnani Dec 12 '19 at 06:03