question

MJ-8053 avatar image
0 Votes"
MJ-8053 asked MJ-8053 commented

is it possible to debug asp.net mvc app on the server?

We provide applicant tracking software for our clients. This is done via the web. We host these websites on our servers. Every now and then one of them will want to view something from the server logs. So I wrote a log analyzer that will read the log files and create the various reports. When I test it on my local machine using the logs from a single specific user it works fine.

So I installed the software to a directory on our servers and then for each client I create an app in IIS that points to the log Analyzer.

Here is some code that says which logs to read in the Controller

  string processID = Request.ServerVariables["INSTANCE_ID"].ToString();
  Logfile log = new Logfile();
  string path = @"C:\inetpub\logs\LogFiles\W3SVC" + processID;
    
 List<String> logs = log.ListLogs((DateTime)StartDate, (DateTime)EndDate);
 List<Logfile> data = await log.GetLogs(logs, path);

and here is the class

 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 using System.Web;
 using System.Web.Caching;
     
    
 namespace Log_Analyser.Models
 {
     public class Logfile
     {
         public string date { get; set; }
         public string time { get; set; }
         public string ServerIP { get; set; }
         public string Method { get; set; }
         public string URI { get; set; }
         public string Query { get; set; }
         public string Port { get; set; }
         public string Username { get; set; }
         public string IP { get; set; }
         public string UserAgent { get; set; }
         public string Referrer { get; set; }
         public string Status { get; set; }
         public string SubStatus { get; set; }
         public string Win32Status { get; set; }
         public string TimeOnSite { get; set; }
         public string Browser { get; set; }
         public string Domain { get; set; }
    
         public List<String> ListLogs(DateTime StartDate, DateTime EndDate)
         {
             Cache c = new Cache();
             List<String> logList = c.Get("FileList") as List<String>;
             if (logList == null)
             {
                 logList = new List<String>();
                 foreach (DateTime day in EachDay(StartDate, EndDate))
                 {
                     string d = day.Day.ToString("00");
                     string m = day.Month.ToString("00");
                     string yr = day.Year.ToString().Substring(2);
                     string logfile = "u_ex" + yr + m + d + ".log";
                     logList.Add(logfile);
                 }
                 c.Insert("FileList", logList, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
             }
             return logList;
         }
    
         public Task<List<Logfile>> GetLogs(List<String> logList, string path)
         {
             Cache cache = new Cache();
             List<Logfile> Files = cache.Get("logFiles") as List<Logfile>;
             if (Files == null)
             {
                 Files = new List<Logfile>();
                 foreach (string log in logList)
                 {
                     string newFile = path + @"\" + log;
                     if (File.Exists(newFile))
                     {
                         using (StreamReader sr = new StreamReader(newFile))
                         {
                             while (sr.Peek() >= 0)
                             {
                                 Logfile lf = new Logfile();
                                 string line = sr.ReadLine();
                                 var info = Regex.Split(line, @"\s+");
                                 if (!info[0].StartsWith("#"))
                                 {
                                     lf.date = info[0];
                                     lf.time = info[1];
                                     lf.ServerIP = info[2];
                                     lf.Method = info[3];
                                     lf.URI = info[4].ToLower();
                                     lf.Query = info[5].ToLower();
                                     lf.Port = info[6];
                                     lf.Username = info[7];
                                     lf.IP = info[8];
                                     lf.UserAgent = info[9];
                                     lf.Referrer = info[10].ToLower();
                                     lf.Domain = ExtractDomainNameFromURL_Method1(lf.Referrer);
                                     lf.Status = info[11];
                                     lf.SubStatus = info[12];
                                     lf.Win32Status = info[13];
                                     lf.TimeOnSite = info[14];
                                     lf.Browser = GetBrowser(info[9].ToUpper());
                                     Files.Add(lf);
                                 }
                             }
                         }
                     }
                 }
                 cache.Insert("logFiles", Files,null, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration);
             }
             return Task.FromResult(Files);
         }
 }


My issue is that when I run on the webserver no information is returned. Is it possible to debug on the server itself?


windows-server-iisdotnet-aspnet-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

CitrusYan-MSFT avatar image
0 Votes"
CitrusYan-MSFT answered MJ-8053 commented

Sure, you can remote debug your ASP.NET app hosted on your servers, please follow the following How-to guide to learn more details:
https://docs.microsoft.com/en-us/visualstudio/debugger/remote-debugging-aspnet-on-a-remote-iis-7-5-computer?view=vs-2019#network-requirements

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you i will use that.

0 Votes 0 ·
DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered MJ-8053 edited

Why don't you use local IIS on your developer machine to debug Web project on local IIS through visual stuido? IIS Express is not IIS on the Web server machine. Local IIS is the Web server on your local machine like IIS is on the Web server machine.

https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-enable-debugging-for-aspnet-applications?view=vs-2017

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

to get it to work in IIS Express on my local machine I cannot use

 string processID = Request.ServerVariables["INSTANCE_ID"].ToString();
 string path = @"c:\inetpub\logs\LogFiles\W3SVC" + processID;

instead I have to say which individual log file to look at like so

 string processID = "74"; 
 string path = @"c:\inetpub\logs\LogFiles\W3SVC" + processID;

Which because i will load in Multiple websites i need it to pull that dynamically. For some reason though it is not reading the log files even though it is putting the correct id in place






0 Votes 0 ·