/// <summary> /// Initializes with a default configuration. /// </summary> /// <remarks> /// <para> /// Initializes the specified repository using a <see cref="T:log4net.Appender.ConsoleAppender" /> /// that will write to <c>Console.Out</c>. The log messages are /// formatted using the <see cref="T:log4net.Layout.PatternLayout" /> layout object /// with the <see cref="F:log4net.Layout.PatternLayout.DetailConversionPattern" /> /// layout style. /// </para> /// </remarks> public static ICollection Configure() { // 创建默认工厂 ILoggerRepository repository; string repositoryName = "log4net-default-repository"; // 默认工厂 if (LoggerManager.Exists(repositoryName)) { repository = LoggerManager.GetRepository(repositoryName); } else { repository = LoggerManager.CreateRepository(repositoryName); } // 进行配置 ArrayList arrayList = new ArrayList(); using (new LogLog.LogReceivedAdapter(arrayList)) { PatternLayout patternLayout = new PatternLayout(); patternLayout.ConversionPattern = "%timestamp [%thread] %level %logger %ndc - %message%newline"; patternLayout.ActivateOptions(); ConsoleAppender consoleAppender = new ConsoleAppender(); consoleAppender.Layout = patternLayout; consoleAppender.ActivateOptions(); InternalConfigure(repository, consoleAppender); } repository.ConfigurationMessages = arrayList; return(arrayList); }
/// <summary> /// Configures the <see cref="T:log4net.Repository.ILoggerRepository" /> using the specified configuration /// file. /// </summary> /// <param name="configFile">The XML file to load the configuration from.</param> /// <remarks> /// <para> /// The configuration file must be valid XML. It must contain /// at least one element called <c>log4net</c> that holds /// the configuration data. /// </para> /// <para> /// The log4net configuration file can possible be specified in the application's /// configuration file (either <c>MyAppName.exe.config</c> for a /// normal application on <c>Web.config</c> for an ASP.NET application). /// </para> /// <para> /// The first element matching <c><configuration></c> will be read as the /// configuration. If this file is also a .NET .config file then you must specify /// a configuration section for the <c>log4net</c> element otherwise .NET will /// complain. Set the type for the section handler to <see cref="T:System.Configuration.IgnoreSectionHandler" />, for example: /// <code lang="XML" escaped="true"> /// <configSections> /// <section name="log4net" type="System.Configuration.IgnoreSectionHandler" /> /// </configSections> /// </code> /// </para> /// <example> /// The following example configures log4net using a configuration file, of which the /// location is stored in the application's configuration file : /// </example> /// <code lang="C#"> /// using log4net.Config; /// using System.IO; /// using System.Configuration; /// /// ... /// /// XmlConfigurator.Configure(new FileInfo(ConfigurationSettings.AppSettings["log4net-config-file"])); /// </code> /// <para> /// In the <c>.config</c> file, the path to the log4net can be specified like this : /// </para> /// <code lang="XML" escaped="true"> /// <configuration> /// <appSettings> /// <add key="log4net-config-file" value="log.config" /> /// </appSettings> /// </configuration> /// </code> /// </remarks> public static ICollection Configure(FileInfo configFile) { // 创建默认工厂 ILoggerRepository repository; string repositoryName = "log4net-default-repository"; // 默认工厂 if (LoggerManager.Exists(repositoryName)) { repository = LoggerManager.GetRepository(repositoryName); } else { repository = LoggerManager.CreateRepository(repositoryName); } // 进行配置 ArrayList arrayList = new ArrayList(); using (new LogLog.LogReceivedAdapter(arrayList)) { InternalConfigure(repository, configFile); } repository.ConfigurationMessages = arrayList; return(arrayList); }
/// <summary> /// Shorthand for <see cref="M:LogManager.GetLogger(string)" />. /// </summary> /// <remarks> /// Get the logger for the fully qualified name of the type specified. /// </remarks> /// <param name="type">The full name of <paramref name="type" /> will be used as the name of the logger to retrieve.</param> /// <returns>The logger with the name specified.</returns> public static ILog GetLogger(Type type) { string repository = "log4net-default-repository"; // 采用默认工厂 if (LoggerManager.Exists(repository)) { return(GetLogger(repository, type.FullName)); } else { return(GetLogger(type.GetTypeInfo().Assembly, type.FullName)); // 默认工厂不存在则根据当前类型所在得程序集创建一个 } }
/// <summary> /// Returns the named logger if it exists /// </summary> /// <remarks> /// <para>If the named logger exists (in the specified assembly's domain) then it /// returns a reference to the logger, otherwise it returns /// <c>null</c>.</para> /// </remarks> /// <param name="assembly">the assembly to use to lookup the domain</param> /// <param name="name">The fully qualified logger name to look for</param> /// <returns>The logger found, or null</returns> public static IExtLog Exists(Assembly assembly, string name) { return(WrapLogger(LoggerManager.Exists(assembly, name))); }
/// <summary> /// Returns the named logger if it exists /// </summary> /// <remarks> /// <para>If the named logger exists (in the specified domain) then it /// returns a reference to the logger, otherwise it returns /// <c>null</c>.</para> /// </remarks> /// <param name="domain">the domain to lookup in</param> /// <param name="name">The fully qualified logger name to look for</param> /// <returns>The logger found, or null</returns> public static IExtLog Exists(string domain, string name) { return(WrapLogger(LoggerManager.Exists(domain, name))); }
/// <summary> /// Returns the named logger if it exists. /// </summary> /// <remarks> /// <para> /// If the named logger exists (in the repository for the specified assembly) then it /// returns a reference to the logger, otherwise it returns /// <c>null</c>. /// </para> /// </remarks> /// <param name="repositoryAssembly">The assembly to use to lookup the repository.</param> /// <param name="name">The fully qualified logger name to look for.</param> /// <returns> /// The logger, or <c>null</c> if the logger doesn't exist in the specified /// assembly's repository. /// </returns> public static ILog Exists(Assembly repositoryAssembly, string name) { return(WrapLogger(LoggerManager.Exists(repositoryAssembly, name))); }
/// <summary> /// Returns the named logger if it exists. /// </summary> /// <remarks> /// <para> /// If the named logger exists (in the specified repository) then it /// returns a reference to the logger, otherwise it returns /// <c>null</c>. /// </para> /// </remarks> /// <param name="repository">The repository to lookup in.</param> /// <param name="name">The fully qualified logger name to look for.</param> /// <returns> /// The logger found, or <c>null</c> if the logger doesn't exist in the specified /// repository. /// </returns> public static IInternalLog Exists(string repository, string name) { return(WrapLogger(LoggerManager.Exists(repository, name))); }
/// <summary> /// Returns the named logger if it exists /// </summary> /// <remarks> /// <para>If the named logger exists (in the specified assembly's domain) then it /// returns a reference to the logger, otherwise it returns /// <c>null</c>.</para> /// </remarks> /// <param name="assembly">the assembly to use to lookup the domain</param> /// <param name="name">The fully qualified logger name to look for</param> /// <returns>The logger found, or null</returns> public static IProgramLog Exists(Assembly assembly, string name) { return(ProgramLogManager.WrapLogger(LoggerManager.Exists(assembly, name))); }
/// <summary> /// Returns the named logger if it exists /// </summary> /// <remarks> /// <para>If the named logger exists (in the specified domain) then it /// returns a reference to the logger, otherwise it returns /// <c>null</c>.</para> /// </remarks> /// <param name="domain">the domain to lookup in</param> /// <param name="name">The fully qualified logger name to look for</param> /// <returns>The logger found, or null</returns> public static IProgramLog Exists(string domain, string name) { return(ProgramLogManager.WrapLogger(LoggerManager.Exists(domain, name))); }
/// <summary> /// Initializes with a default configuration. /// </summary> /// <param name="useConsole">是否输出到控制台</param> /// <param name="useFile">是否输出到文件</param> /// <returns></returns> public static ICollection Configure(bool useConsole = false, bool useFile = true) { // 创建默认工厂 ILoggerRepository repository; string repositoryName = "log4net-default-repository"; // 默认工厂 if (LoggerManager.Exists(repositoryName)) { repository = LoggerManager.GetRepository(repositoryName); } else { repository = LoggerManager.CreateRepository(repositoryName); } // 进行配置 ArrayList arrayList = new ArrayList(); using (new LogLog.LogReceivedAdapter(arrayList)) { // 日志输出格式, 各种参数设置参见PatternLayout.cs: // %m(message): 输出的日志消息; // %n(newline): 换行; // %d(datetime): 输出当前语句运行的时刻; // %r(runtime): 输出程序从运行到执行到当前语句时消耗的毫秒数; // %t(threadid): 当前语句所在的线程ID; // %p(priority): 日志的当前日志级别; // %c(class): 当前日志对象的名称; // %L: 输出语句所在的行号; // %F: 输出语句所在的文件名; // %-10: 表示最小长度为10,如果不够,则用空格填充; PatternLayout patternLayout = new PatternLayout(); //patternLayout.ConversionPattern = "--------时间: %d, 耗时: %r(ms), 类型: %-5p --------%n对象: %c%n消息: %m%n%n"; //patternLayout.ConversionPattern = "--------时间: %d, 耗时: %r(ms), 类型: %-5p --------%n对象: %c%n消息: %m%n详情: %exception%n%n"; patternLayout.ConversionPattern = "--------时间: %d, 类型: %-5p --------%n%property{source}%property{class}%property{method}%property{message}%property{detail}%property{data}%n"; patternLayout.ActivateOptions(); #region 文件输出器 RollingFileAppender fileAppender = null; if (useFile) { fileAppender = new RollingFileAppender(); // 日志文件名, // 如果RollingStyle为Composite或Date, 则这里一般设置成目录名, 文件名在DatePattern里设置; // 如果RollingStyle为其他滚动方式, 则必须设置成文件名 fileAppender.File = "log\\"; // 是否是静态的日志文件名, 即固定的日志文件名称, // 如果你想要动态生成日志文件名,例如将RollingStyle设置成Date或Composite, 以让系统自动生成日志文件的名称, // 那么你必须将staticLogFileName设置成false fileAppender.StaticLogFileName = false; // 日志滚动方式,可设置成: // Size(按文件), 此时你应该将file的值设置成一个固定的名称, 例如: test.log 或 log\\test.log 或 c:\\log\\test.log; // Date(按日期), 此时你应该将file的值设置成一个目录或者空字符串, 如果设置成空字符串, 系统将把日志记录在当前应用程序所在的目录中; // Composite(按日期及文件),默认为Composite fileAppender.RollingStyle = RollingFileAppender.RollingMode.Composite; // 日志文件名格式, // 当RollingStyle为Composite或Date, 在此处设置日志文件名的格式时, // 固定不变的部分用单引号括起来, 其它部分则设置成日期格式 fileAppender.DatePattern = "yyyyMMdd'.log'"; // 日志记录是否追加到文件, // 默认为true, 表示将记录追加到文件尾部; flase, 表示覆盖已有的日志文件记录 fileAppender.AppendToFile = true; // 单个日志文件的最大尺寸, // 可用的单位有: KB | MB | GB, 默认为字节(不带单位) fileAppender.MaximumFileSize = "2MB"; //每日最多记录的日志文件个数 fileAppender.MaxSizeRollBackups = 10; // 单个日志文件超限后日志备份方式, 默认值为 - 1, // 当日志文件超过MaximumFileSize大小时,系统根据CountDirection的值来备份日志文件: // (1)当此值设置成 > -1时, 则根据file里指定的文件名依次按0,1,2...递增创建日志备份, 直到数量等于MaxSizeRollBackups参数值为止, // 以后的日志记录则会根据maximumFileSize循环写入file, 当filesize > maximumFileSize, 进行一次新的循环写入时, 会将file记录写入备份日志, 并对备份日志进行重新编号; // (2)当此值设置成 <= -1时, 则根据file里指定的文件名依次按0,1,2...递增创建日志备份, 直到数量等于MaxSizeRollBackups参数值为止, // 以后的日志记录则会根据maximumFileSize循环写入file, 当filesize > maximumFileSize, 进行一次新的循环写入时, 不会将file记录写入备份日志, 即备份日志被固化不受影响-- > fileAppender.CountDirection = -1; fileAppender.Layout = patternLayout; fileAppender.ActivateOptions(); } #endregion #region 控制台输出器 //ConsoleAppender consoleAppender = null; //if (useConsole) //{ // consoleAppender = new ConsoleAppender(); // consoleAppender.Layout = patternLayout; // consoleAppender.ActivateOptions(); //} #endregion #region 颜色控制台输出器 ManagedColoredConsoleAppender coloredConsoleAppender = null; if (useConsole) { coloredConsoleAppender = new ManagedColoredConsoleAppender(); // 设置日志级别, 默认值为DEBUG, // 级别由低到高依次为: ALL | DEBUG < INFO < WARN < ERROR < FATAL | OFF.其中: // ALL表示记录所有日志; // OFF表示不记录日志, 即关闭日志记录; // 其它则按级别记录,例如级别设置成WARN,则低于WARN级别的INFO和DEBUG日志将不会被记录, 其它依次类推. var debugMap = new LevelColors { ForeColor = ConsoleColor.White, Level = Level.Debug }; var infoMap = new LevelColors { ForeColor = ConsoleColor.Green, Level = Level.Info }; var warnMap = new LevelColors { ForeColor = ConsoleColor.Yellow, Level = Level.Warn }; var errorMap = new LevelColors { ForeColor = ConsoleColor.Red, Level = Level.Error }; var fatalMap = new LevelColors { ForeColor = ConsoleColor.Magenta, Level = Level.Fatal }; debugMap.ActivateOptions(); infoMap.ActivateOptions(); warnMap.ActivateOptions(); errorMap.ActivateOptions(); fatalMap.ActivateOptions(); coloredConsoleAppender.AddMapping(debugMap); coloredConsoleAppender.AddMapping(warnMap); coloredConsoleAppender.AddMapping(infoMap); coloredConsoleAppender.AddMapping(errorMap); coloredConsoleAppender.AddMapping(fatalMap); coloredConsoleAppender.Layout = patternLayout; coloredConsoleAppender.ActivateOptions(); } #endregion var appenders = new List <IAppender>(); if (useFile) { appenders.Add(fileAppender); } if (useConsole) { appenders.Add(coloredConsoleAppender); } if (appenders.Count > 0) { InternalConfigure(repository, appenders.ToArray()); } } repository.ConfigurationMessages = arrayList; return(arrayList); }
/// <summary> /// Returns the named logger if it exists. /// </summary> /// <param name="domainAssembly">The assembly to use to lookup the domain.</param> /// <param name="name">The fully qualified logger name to look for.</param> /// <returns> /// The logger, or <c>null</c> if the logger doesn't exist in the specified /// assembly's domain. /// </returns> public static ILog Exists(Assembly domainAssembly, string name) { return(WrapLogger(LoggerManager.Exists(domainAssembly, name))); }
public static ILog Exists(string repository, string name) => WrapLogger(LoggerManager.Exists(repository, name));
/// <summary> /// Liefert den angegebenen Logger <paramref name="name"/> falls er existiert. /// </summary> /// <remarks> /// <para>Falls der angegebene Logger (in der Default-Hierarchie) existiert, wird das /// entsprechende Logger-Objekt geliefert, sonst <c>null</c>.</para> /// </remarks> /// <param name="theAssembly">Die Assembly, in der gesucht werden soll.</param> /// <param name="name">Der voll-qualifizierte Name des Loggers.</param> /// <returns>Den entsprechenden Logger oder null.</returns> public static ITraceLog Exists(Assembly theAssembly, string name) { return(WrapLogger(LoggerManager.Exists(theAssembly, name))); }
/// <summary> /// Returns the named logger if it exists. /// </summary> /// <param name="repository">The repository to lookup in.</param> /// <returns></returns> public static bool Exists(string repository) { return(LoggerManager.Exists(repository)); }