Created
May 6, 2015 10:04
-
-
Save mtmk/659bf991b03e2941cfa0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Program | |
{ | |
static void Main() | |
{ | |
Logger.Init(); | |
var log = Logger.Get("main"); | |
log.Info("Logging now.."); | |
} | |
} | |
public static class Logger | |
{ | |
const string RepoName = "app.log"; | |
static readonly ILoggerRepository Repo = LogManager.CreateRepository(RepoName); | |
static Logger() | |
{ | |
InitLog4Net(); | |
InitAppDomainEventLogs(); | |
InitTraceLogs(); | |
} | |
public static void Init() | |
{ | |
// load type and run constructor | |
} | |
public static ILog Get(string name) | |
{ | |
return LogManager.GetLogger(RepoName, name); | |
} | |
private static void InitLog4Net() | |
{ | |
int backups = 10; | |
string maximumFileSize = "10MB"; | |
string logPath = "logs"; | |
Repo.ConfigureFile(Path.Combine(logPath, "info.log"), Level.Info, backups, maximumFileSize); | |
Repo.ConfigureFile(Path.Combine(logPath, "warn.log"), Level.Warn, backups, maximumFileSize); | |
Repo.ConfigureFile(Path.Combine(logPath, "error.log"), Level.Error, backups, maximumFileSize); | |
Repo.EndConfigure(true); | |
} | |
static void InitAppDomainEventLogs() | |
{ | |
var logger = LogManager.GetLogger(RepoName, "AppDomain.CurrentDomain"); | |
logger.Info(new string('_', 80)); | |
logger.InfoFormat("Starting.. v{0} (CLR {1})", Assembly.GetExecutingAssembly().GetName().Version, | |
Assembly.GetExecutingAssembly().ImageRuntimeVersion); | |
AppDomain.CurrentDomain.UnhandledException += (s, e) => | |
{ | |
logger.ErrorFormat("UnhandledException: IsTerminating={0}", e.IsTerminating); | |
logger.Error(e.ExceptionObject); | |
}; | |
AppDomain.CurrentDomain.AssemblyLoad += | |
(s, e) => | |
logger.InfoFormat("LoadedAssembly: FullName='{0}' Location='{1}'", e.LoadedAssembly.FullName, | |
e.LoadedAssembly.Location); | |
} | |
static void InitTraceLogs() | |
{ | |
Trace.Listeners.Add(new LogTraceListener()); | |
} | |
} | |
public static class Loggerx | |
{ | |
const string LogPattern = "%d [%t] %-5p %c - %m%n"; | |
public static void ConfigureFile(this ILoggerRepository repo, string logFile, Level level, | |
int backups, string maximumFileSize) | |
{ | |
var patternLayout = new PatternLayout { ConversionPattern = LogPattern }; | |
patternLayout.ActivateOptions(); | |
var hierarchy = (Hierarchy)repo; | |
AppenderSkeleton roller = new RollingFileAppender | |
{ | |
Layout = patternLayout, | |
AppendToFile = true, | |
RollingStyle = RollingFileAppender.RollingMode.Size, | |
MaximumFileSize = maximumFileSize, | |
StaticLogFileName = true, | |
File = logFile, | |
MaxSizeRollBackups = backups, | |
}; | |
roller.AddFilter(new LevelMatchFilter { LevelToMatch = level }); | |
roller.AddFilter(new DenyAllFilter()); | |
roller.ActivateOptions(); | |
hierarchy.Root.AddAppender(roller); | |
} | |
internal static void EndConfigure(this ILoggerRepository repo, bool addConsole) | |
{ | |
var hierarchy = (Hierarchy)repo; | |
if (addConsole) | |
{ | |
var patternLayout = new PatternLayout { ConversionPattern = LogPattern }; | |
patternLayout.ActivateOptions(); | |
AppenderSkeleton tracer = new ConsoleAppender { Layout = patternLayout }; | |
//tracer.AddFilter(new LevelMatchFilter { LevelToMatch = Level.All }); | |
//tracer.AddFilter(new DenyAllFilter()); | |
tracer.ActivateOptions(); | |
hierarchy.Root.AddAppender(tracer); | |
} | |
hierarchy.Root.Level = Level.All; | |
hierarchy.Configured = true; | |
} | |
} | |
[HostProtection(Synchronization = true)] | |
public class LogTraceListener : TraceListener | |
{ | |
private readonly ILog _log; | |
public LogTraceListener() | |
{ | |
_log = Logger.Get("TraceListener"); | |
} | |
public override void Write(string message) | |
{ | |
if (_log == null) return; | |
_log.Info(message); | |
} | |
public override void WriteLine(string message) | |
{ | |
if (_log == null) return; | |
_log.Info(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment