Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different Ways To Get The Server Name In ASP.NET

I want to log my server name in ASP.NET Application, I use multi-servers and load balancing so I need to log the server name.

But what is the difference between these ways to get the server name?

and which one is true or better to log?

any idea?

or any other ways?

System.Environment.MachineName
Server.MachineName
System.Net.Dns.GetHostName()

There is also another ways but not always return correct server name:

Request.ServerVariables["SERVER_NAME"]
System.Net.Dns.GetHostEntry(Request.ServerVariables("SERVER_NAME")).HostName
System.Net.Dns.GetHostEntry(Request.ServerVariables("LOCAL_ADDR")).HostName
like image 288
Saeid Alizade Avatar asked Feb 20 '14 06:02

Saeid Alizade


1 Answers

I know this is an old question. I was searching for answer to the same issue; here is my solution.

string url = HttpContext.Current.Request.Url.ToString();
string[] tempArray = url.Split('/');
string serverName = tempArray[0] + "/" + tempArray[1] + "/" + tempArray[2] ;

This way you will get the exact server name.

like image 109
shreesha Avatar answered Nov 09 '22 16:11

shreesha