Exemple #1
0
        async public Task LogAsync(string mapName, loggingMethod loggingMethod, string message)
        {
            try
            {
                mapName = mapName?.ToLower() ?? String.Empty;

                if (!String.IsNullOrWhiteSpace(_mapServerService.Options.LoggingRootPath))
                {
                    string fileName = await LogFilename(mapName, loggingMethod, true);

                    await File.AppendAllLinesAsync(fileName,
                                                   new string[]
                    {
                        DateTime.UtcNow.ToShortDateString() + " " + DateTime.UtcNow.ToLongTimeString() + " (UTC) " + message,
                        "-"
                    });
                }
                else
                {
                    Console.WriteLine(DateTime.UtcNow.ToShortDateString() + " " + DateTime.UtcNow.ToLongTimeString() + " (UTC) " + loggingMethod.ToString() + ": " + message);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Logging error: " + ex.Message);
            }
        }
Exemple #2
0
        public static void Log(loggingMethod method, string file_postfix, string text)
        {
            string procName = global::System.Diagnostics.Process.GetCurrentProcess().ProcessName.Replace(".", "_");

            string filename = SystemVariables.MyCommonApplicationData + @"\Logfiles\" + procName + @"\";

            switch (method)
            {
            case loggingMethod.request:
                filename += procName + ((!String.IsNullOrEmpty(file_postfix)) ? "_" + file_postfix : "") + "_Requests.log";
                break;

            case loggingMethod.request_detail_pro:
            case loggingMethod.request_detail:
                filename += procName + ((!String.IsNullOrEmpty(file_postfix)) ? "_" + file_postfix : "") + "_Request_Details.log";
                text     += "\nend\n";
                break;

            case loggingMethod.error:
                filename += procName + ((!String.IsNullOrEmpty(file_postfix)) ? "_" + file_postfix : "") + "_Errors.log";
                break;
            }

            Log(filename, text);
        }
Exemple #3
0
        async public Task LogAsync(IServiceRequestContext context, string header, loggingMethod method, string msg)
        {
            if (!LoggingEnabled(method))
            {
                return;
            }

            await LogAsync(ToMapName(context?.ServiceRequest?.Service, context?.ServiceRequest?.Folder), header, method, msg);
        }
Exemple #4
0
        async public Task <bool> LogFileExists(string mapName, loggingMethod loggingMethod)
        {
            string fileName = await LogFilename(mapName, loggingMethod);

            if (!String.IsNullOrEmpty(fileName))
            {
                FileInfo fi = new FileInfo(fileName);
                return(fi.Exists);
            }

            return(false);
        }
Exemple #5
0
        public void Log(string header, loggingMethod method, string msg)
        {
            switch (method)
            {
            case loggingMethod.error:
                if (_log_errors)
                {
                    if (!String.IsNullOrEmpty(header))
                    {
                        msg = header + "\n" + msg;
                    }
                    Logger.Log(method, IMS.Port != 0 ? IMS.Port.ToString() : String.Empty, msg);
                }
                break;

            case loggingMethod.request:
                if (_log_requests)
                {
                    if (!String.IsNullOrEmpty(header))
                    {
                        msg = header + " - " + msg;
                    }
                    Logger.Log(method, IMS.Port != 0 ? IMS.Port.ToString() : String.Empty, msg);
                }
                break;

            case loggingMethod.request_detail:
                if (_log_request_details)
                {
                    if (!String.IsNullOrEmpty(header))
                    {
                        msg = header + "\n" + msg;
                    }
                    Logger.Log(method, IMS.Port != 0 ? IMS.Port.ToString() : String.Empty, msg);
                }
                break;

            case loggingMethod.request_detail_pro:
                if (_log_request_details)
                {
                    if (!String.IsNullOrEmpty(header))
                    {
                        header = header.Replace(".", "_");
                    }
                    Logger.Log(method,
                               (IMS.Port != 0 ? IMS.Port.ToString() : String.Empty) + "_" + header,
                               msg);
                }
                break;
            }
        }
Exemple #6
0
        async public Task LogAsync(string mapName, string header, loggingMethod method, string msg)
        {
            if (!LoggingEnabled(method))
            {
                return;
            }

            switch (method)
            {
            case loggingMethod.error:
                if (!String.IsNullOrEmpty(header))
                {
                    msg = header + "\n" + msg;
                }

                await _logger.LogAsync(mapName, method, msg);

                break;

            case loggingMethod.request:
                if (!String.IsNullOrEmpty(header))
                {
                    msg = header + " - " + msg;
                }

                await _logger.LogAsync(mapName, method, msg);

                break;

            case loggingMethod.request_detail:
                if (!String.IsNullOrEmpty(header))
                {
                    msg = header + "\n" + msg;
                }

                await _logger.LogAsync(mapName, method, msg);

                break;

            case loggingMethod.request_detail_pro:
                if (!String.IsNullOrEmpty(header))
                {
                    header = header.Replace(".", "_");
                }

                await _logger.LogAsync(mapName, method, msg);

                break;
            }
        }
        public bool LoggingEnabled(loggingMethod method)
        {
            switch (method)
            {
            case loggingMethod.error:
                return(_log_errors);

            case loggingMethod.request:
                return(_log_requests);

            case loggingMethod.request_detail:
            case loggingMethod.request_detail_pro:
                return(_log_request_details);
            }

            return(false);
        }
Exemple #8
0
        public (IEnumerable <string> errors, long ticks) ErrorLogs(string mapName, loggingMethod loggingMethod, long last = 0)
        {
            if (!String.IsNullOrWhiteSpace(_mapServerService.Options.LoggingRootPath))
            {
                var dir = new DirectoryInfo(_mapServerService.Options.LoggingRootPath + "/" + mapName);
                if (dir.Exists)
                {
                    var fileNames = dir.GetFiles("*.log").Select(f => f.Name.Substring(0, f.Name.Length - f.Extension.Length)).OrderByDescending(f => f);
                    foreach (var fileName in fileNames)
                    {
                        try
                        {
                            var ticks = long.Parse(fileName.Split('-')[1]);
                            if (last == 0 || last > ticks)
                            {
                                List <string> errors = new List <string>();

                                using (StreamReader fileStream = File.OpenText(dir.FullName + "/" + fileName + ".log"))
                                {
                                    string        line;
                                    StringBuilder message = new StringBuilder();
                                    while ((line = fileStream.ReadLine()) != null)
                                    {
                                        if (line.StartsWith("-"))
                                        {
                                            errors.Add(message.ToString().Trim());
                                            message.Clear();
                                        }
                                        else
                                        {
                                            message.Append(line + "\n");
                                        }
                                    }
                                }

                                errors.Reverse();
                                return(errors, ticks);
                            }
                        }
                        catch { }
                    }
                }
            }

            return(new string[0], 0);
        }
Exemple #9
0
        async public Task <string> LogFilename(string mapName, loggingMethod loggingMethod, bool createDir = false)
        {
            if (!String.IsNullOrWhiteSpace(_mapServerService.Options.LoggingRootPath))
            {
                mapName = mapName?.ToLower() ?? String.Empty;

                var dir = new DirectoryInfo(_mapServerService.Options.LoggingRootPath + "/" + mapName);
                if (createDir && !dir.Exists)
                {
                    dir.Create();
                }

                var mapService = String.IsNullOrWhiteSpace(mapName) ? null : _mapServerService.MapServices.Where(s => s.Fullname?.ToLower() == mapName).FirstOrDefault();

                var ticks = (mapService != null && mapService.RunningSinceUtc.HasValue) ? mapService.RunningSinceUtc.Value.Ticks : DateTime.UtcNow.Ticks;

                if (mapService != null)
                {
                    if (!mapService.RunningSinceUtc.HasValue)
                    {
                        var settings = await mapService?.GetSettingsAsync();

                        if (settings != null)
                        {
                            ticks = settings.RefreshService.Ticks;
                        }
                    }
                    else
                    {
                        ticks = mapService.RunningSinceUtc.Value.Ticks;
                    }
                }

                string fileName = $"{ loggingMethod.ToString() }-{ ticks.ToString().PadLeft(21, '0') }.log";

                return(dir + "/" + fileName);
            }

            return(String.Empty);
        }
Exemple #10
0
 async public Task LogAsync(IServiceRequestContext context, loggingMethod loggingMethod, string message)
 {
     await LogAsync(ToMapName(context), loggingMethod, message);
 }
Exemple #11
0
 public static void Log(loggingMethod method, string text)
 {
     Log(method, String.Empty, text);
 }
Exemple #12
0
 static public void Log(loggingMethod loggingMethod, string message)
 {
     Console.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + " " + loggingMethod.ToString() + ": " + message);
 }