public void GetServerLogFiles(string path) { var filenames = Directory.GetFiles(path); var logfiles = new List <ServerLogFile>(); try { foreach (var filename in filenames) { var files = File.ReadAllLines(filename); foreach (var file in files) { if (!file.StartsWith("#")) { var logfile = new ServerLogFile(); var splittedfile = file.Split(' '); if (!splittedfile[8].Contains(":") && !splittedfile[8].StartsWith("192.168") && !splittedfile[8].StartsWith("127.0.0.1")) { logfile.TimeStamp = Convert.ToDateTime(splittedfile[0] + " " + splittedfile[1]); logfile.ServerIP = splittedfile[2]; logfile.Method = splittedfile[3]; logfile.UriQuery = splittedfile[4]; logfile.UriStem = splittedfile[5]; logfile.ServerPort = splittedfile[6]; logfile.Username = splittedfile[7]; logfile.ClientIP = splittedfile[8]; logfile.ClientUserAgent = splittedfile[9]; logfile.Status = splittedfile[10]; logfile.SubStatus = splittedfile[11]; logfile.Win32Status = splittedfile[12]; logfile.TimeTaken = splittedfile[13]; if (this._serverlogrepository.Find(l => l.ClientIP.Equals(logfile.ClientIP) && l.ClientUserAgent.Equals(logfile.ClientUserAgent) && l.TimeStamp.Equals(logfile.TimeStamp) && l.Method.Equals(logfile.Method) && l.TimeTaken.Equals(logfile.TimeTaken) && l.UriQuery.Equals(logfile.UriQuery) && l.Status.Equals(logfile.Status) && l.SubStatus.Equals(logfile.SubStatus)) .Count() == 0) { this.SetLocation(logfile).Wait(); } } } } } } catch { } }
public async Task SetLocation(ServerLogFile file) { bool locationalreadyset = false; var iplocation = new IPLocation(); if (this.locationsduringruntime.Where(l => l.ipAddress.Equals(file.ClientIP)).Count() > 0) { iplocation = this.locationsduringruntime.Where(l => l.ipAddress.Equals(file.ClientIP)).First(); locationalreadyset = true; } if (!locationalreadyset) { if (this._iplocationrepository.Find(l => l.ipAddress.Equals(file.ClientIP)).Count() > 0) { iplocation = this._iplocationrepository.Find(l => l.ipAddress.Equals(file.ClientIP)).First(); locationalreadyset = true; } if (!locationalreadyset && this._serverlogrepository.IsIPAdressAlreadyComplete(file.ClientIP)) { iplocation = this._serverlogrepository.GetIPLocationOfIPAddress(file.ClientIP); locationalreadyset = true; } } if (!file.ClientIP.Contains(":") && !file.ClientIP.StartsWith("192.168") && !file.ClientIP.StartsWith("127.0.0.1") && !locationalreadyset) { var client = new HttpClient(); try { client.BaseAddress = new Uri("http://api.ipinfodb.com"); var response = await client.GetAsync(string.Format("/v3/ip-city/?key={0}&ip={1}&format=json", apikey, file.ClientIP)); response.EnsureSuccessStatusCode(); var stringResponse = await response.Content.ReadAsStringAsync(); iplocation = JsonConvert.DeserializeObject <IPLocation>(stringResponse); this.locationsduringruntime.Add(iplocation); this._iplocationrepository.Add(iplocation); } catch (System.Net.Http.HttpRequestException ex) { Debug.WriteLine(ex.Message + " Can not get IP: " + file.ClientIP); } catch (System.Exception ex) { Debug.WriteLine(ex.Message); } } file.City = iplocation.cityName ?? ""; file.Country = iplocation.countryName ?? ""; file.Longitude = iplocation.longitude ?? ""; file.Latitude = iplocation.latitude ?? ""; if (!this._serverlogrepository.DoesEntryAlreadyExist(file)) { this._serverlogrepository.Add(file); } else { var entry = this._serverlogrepository.Get(s => s.ID.Equals(file.ID)); if (entry.LocationID < 0) { this._serverlogrepository.Remove(entry); this._serverlogrepository.Add(file); } } }