static Log4netIntegration() { try { var confFile = new FileInfo(HostApp.GetPath("log4net.config")); if (confFile.Exists) { XmlConfigurator.ConfigureAndWatch(LogRepository, confFile); } else { Configure(); } //检查Logs目录是否存在。docker下测试时无法自动创建。 var logPath = HostApp.GetPath(LogDir); if (!Directory.Exists(logPath)) { Directory.CreateDirectory(logPath); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
public NullLogger() { //检查Logs目录是否存在。 LogsDir = HostApp.GetPath("Logs"); if (!Directory.Exists(LogsDir)) { Directory.CreateDirectory(LogsDir); } }
//默认配置代码 private static void Configure() { #region Appender RollingFileAppender CreateAppender(string file, int maxBackups) => new RollingFileAppender { Name = $"{file}Appender", File = HostApp.GetPath($"{LogDir}{Path.DirectorySeparatorChar}{file}.log"), RollingStyle = RollingFileAppender.RollingMode.Composite, DatePattern = "yyyy-MM-dd'.log'", MaximumFileSize = "200MB", MaxSizeRollBackups = maxBackups, //StaticLogFileName = false, AppendToFile = true, LockingModel = new FileAppender.MinimalLock(), Layout = GetLayout() }; var errorAppender = CreateAppender("error", 60); errorAppender.AddFilter(new LevelRangeFilter { LevelMin = Level.Error, LevelMax = Level.Fatal }); errorAppender.ActivateOptions(); var infoAppender = CreateAppender("info", 30); infoAppender.AddFilter(new LevelRangeFilter { LevelMin = Level.Info, LevelMax = Level.Warn }); infoAppender.ActivateOptions(); var debugAppender = CreateAppender("debug", 30); debugAppender.AddFilter(new LevelMatchFilter { LevelToMatch = Level.Debug }); debugAppender.AddFilter(new DenyAllFilter()); debugAppender.ActivateOptions(); var hierarchy = (Hierarchy)LogRepository; hierarchy.Root.AddAppender(errorAppender); hierarchy.Root.AddAppender(infoAppender); hierarchy.Root.AddAppender(debugAppender); #endregion hierarchy.Root.Level = Level.Debug; hierarchy.Configured = true; }
/// <summary> /// 导出Http请求信息并保存为文件 /// </summary> internal static void DumpRequestToFile(HttpRequest request, string dirName) { try { //var path = HostApp.GetPath("Logs/PayNotify_" + GatewayType); var path = HostApp.GetPath("Logs/" + dirName); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } path = Path.Combine(path, $"notify_{DateTime.Now:yyyy-MM-dd HH-mm-ss}.log"); File.AppendAllText(path, DumpRequest(request), Encoding.UTF8); } catch (Exception ex) { LogService.Error(ex); } }
internal static string InnerGetAppSet(string key) { if (_configuration == null) { var builder = new ConfigurationBuilder().SetBasePath(HostApp.RootPath); var setFile = HostApp.GetPath(ConfSetFile); if (File.Exists(setFile)) { builder.AddJsonFile(setFile, optional: false, reloadOnChange: true); } else { LogService.WarnFormat($"缺少本地配置文件{ConfSetFile}"); } //.AddJsonFile($"appsettings.{EnvironmentName}.json", optional: true, reloadOnChange: true) //.AddEnvironmentVariables(); _configuration = builder.Build(); } return(_configuration[key]); }