Log4.net custom log file name, log4.net Log File

Source: Internet
Author: User
Tags log4net

Log4.net custom log file name, log4.net Log File

In a plug-in project, a log file is generated for each plug-in or method to facilitate troubleshooting.

Source Code address: https://github.com/xlb378917466/SharpHttpServerCase.git

Using System; using System. collections. generic; using System. linq; using System. text; using System. collections. concurrent; using System. configuration; using log4net; using log4net. appender; using log4net. core; using log4net. layout; using log4net. repository; using log4net. repository. hierarchy; [assembly: log4net. config. xmlConfigurator (Watch = true)] namespace TechSvr. utils {public static class CustomRollingFileL Ogger {private static readonly ConcurrentDictionary <string, ILog> loggerContainer = new ConcurrentDictionary <string, ILog> (); // The default configuration is private const int MAX_SIZE_ROLL_BACKUPS = 20; private const string LAYOUT_PATTERN = "% newline record time: % date % Description: % message % newline"; private const string DATE_PATTERN = "yyyyMMdd "; private const string MAXIMUM_FILE_SIZE = "2 MB"; private const string LEVEL = "ALL"; public Static ILog GetCustomLogger (string loggerName, string category = null, bool additiiner = false) {return loggerContainer. getOrAdd (loggerName, delegate (string name) {RollingFileAppender newAppender = GetNewFileApender (loggerName, GetFile (category, loggerName), delimiter, true, true, delimiter, RollingFileAppender. rollingMode. composite, DATE_PATTERN, LAYOUT_PATTERN); log4net. re Pository. hierarchy. hierarchy repository = (log4net. repository. hierarchy. hierarchy) LogManager. getRepository (); Logger logger = repository. loggerFactory. createLogger (repository, loggerName); logger. hierarchy = repository; logger. parent = repository. root; logger. level = GetLoggerLevel (LEVEL); logger. additi.pdf = additi.pdf; logger. addAppender (newAppender); logger. repository. configured = true; return New LogImpl (logger) ;}) ;}// if no file path is specified, Log \ Alibaba loggername.txt private static string GetFile (string category, string loggerName) is created in the running path) {if (string. isNullOrEmpty (category) {return string. format (@ "Logs \ 02.16.txt", loggerName);} else {return string. format (@ "Logs \ {0 }\{ 12.16.txt", category, loggerName) ;}} private static Level GetLoggerLevel (string level) {if (! String. isNullOrEmpty (level) {switch (level. toLower (). trim () {case "debug": return Level. debug; case "info": return Level. info; case "warn": return Level. warn; case "error": return Level. error; case "fatal": return Level. fatal ;}} return Level. debug;} private static RollingFileAppender GetNewFileApender (string appenderName, string file, int folder, bool appendToFile = true, bool staticLogFileName = false, string maximumFileSize = "2 MB", RollingFileAppender. rollingMode rollingMode = RollingFileAppender. rollingMode. size, string datePattern = "yyyyMMdd \". txt \ "", string layoutPattern = "% d [% t] %-5 p % c-% m % n") {RollingFileAppender appender = new RollingFileAppender {LockingModel = new FileAppender. minimalLock (), Name = appenderName, File = file, AppendToFile = appendToFile, Token = success, Token = success, StaticLogFileName = staticLogFileName, RollingStyle = rollingMode, DatePattern = datePattern }; patternLayout layout = new PatternLayout (layoutPattern); appender. layout = layout; layout. activateOptions (); appender. activateOptions (); return appender ;}}}

Usage

  public static Log GetLogger(string filename = "Log")        {            ILog logger = CustomRollingFileLogger.GetCustomLogger(filename, DateTime.Now.ToString("yyyyMMdd"));            return new Log(logger);        }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.