public void Log(string message, LoggerStatus status = LoggerStatus.Information) { var statusText = status switch { LoggerStatus.Information => "INFO", LoggerStatus.Warning => "WARNING", LoggerStatus.Error => "ERROR", _ => "UNKNOWN" }; var logMessage = $"[{DateTime.Now:yyyy-MM-dd @ HH:mm:ss} * {statusText}] {message}"; Console.WriteLine(logMessage); _writer?.WriteLine(logMessage); } }
/// <summary> /// /// </summary> /// <param name="requestParameter">请求参数</param> /// <param name="responseParameter">响应参数</param> /// <param name="success">响应结果1、成功,2、异常,0、其他</param> /// <param name="userInfo">用户数据</param> private static void Add(LoggerType loggerType, string requestParameter, string responseParameter, string ex, LoggerStatus status) { Sys_Log log = null; try { HttpContext context = Utilities.HttpContext.Current; if (context.Request.Method == "OPTIONS") { return; } ActionObserver cctionObserver = (context.RequestServices.GetService(typeof(ActionObserver)) as ActionObserver); //如果当前请求已经写过日志就不再写日志 //if (cctionObserver.IsWrite) return; //cctionObserver.IsWrite = true; if (context == null) { WriteText($"未获取到httpcontext信息,type:{loggerType.ToString()},reqParam:{requestParameter},respParam:{responseParameter},ex:{ex},success:{status.ToString()}"); return; } UserInfo userInfo = UserContext.Current.UserInfo; log = new Sys_Log() { BeginDate = cctionObserver.RequestDate, EndDate = DateTime.Now, User_Id = userInfo.User_Id, UserName = userInfo.UserTrueName, Role_Id = userInfo.Role_Id, LogType = loggerType.ToString(), ExceptionInfo = ex, RequestParameter = requestParameter, ResponseParameter = responseParameter, Success = (int)status }; SetServicesInfo(log, context); } catch (Exception exception) { log = log ?? new Sys_Log(); log.ExceptionInfo = exception.Message; } if (log == null) { return; } lock (_logger) { loggerQueueData.Enqueue(log); } }
/// <summary> /// Defines the entry point of the application. /// </summary> /// <param name="args">Input parameters.</param> public static void Main(string[] args) { string validationRules = DefaultValidationRules; ServiceType serviceType = ServiceType.Memory; MeterStatus meter = MeterStatus.Off; LoggerStatus logger = LoggerStatus.Off; var parser = new Parser(with => with.CaseInsensitiveEnumValues = true); Parser.Default.ParseArguments <Options>(args) .WithParsed <Options>(o => { validationRules = o.Validate.Equals(CustomValidationType, StringComparison.InvariantCultureIgnoreCase) ? CustomValidationType : DefaultValidationRules; serviceType = (o.Storage == ServiceType.File) ? ServiceType.File : ServiceType.Memory; meter = (o.Meter == MeterStatus.On) ? MeterStatus.On : MeterStatus.Off; logger = (o.Logger == LoggerStatus.On) ? LoggerStatus.On : LoggerStatus.Off; }); parser?.Dispose(); fileCabinetService = CreateServise(validationRules, serviceType, out converter, out validator); if (meter == MeterStatus.On) { fileCabinetService = new ServiceMeter(fileCabinetService, Write); } if (logger == LoggerStatus.On) { fileCabinetService = new ServiceLogger(fileCabinetService); } var commandHandler = CreateCommandHandlers(); Write($"File Cabinet Application, developed by {Program.DeveloperName}" + Environment.NewLine + $"Using {validationRules} validation rules." + Environment.NewLine + $"The {serviceType.ToString()} service."); Write(HintMessage + Environment.NewLine); do { Console.Write("> "); var inputs = Console.ReadLine().Split(' ', 2); const int commandIndex = 0; var command = inputs[commandIndex]; const int parametersIndex = 1; var parameters = inputs.Length > 1 ? inputs[parametersIndex] : string.Empty; try { commandHandler.Handle(new AppCommandRequest(command, parameters)); } catch (ArgumentNullException anex) { Write($"Error. {anex.Message}"); } catch (ArgumentException aex) { Write($"Error. {aex.Message}"); } catch (InvalidOperationException ioex) { Write($"Error. {ioex.Message}"); } }while (isRunning); }
public static void Append(string message, LoggerStatus loggerStatus) { }