-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MultiWindow-NetCore-Demo - log4net logging; InstanceValidator from Gl…
…ueNetCore.0.0.45
- Loading branch information
1 parent
282ce65
commit ef976a4
Showing
4 changed files
with
139 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<configuration> | ||
<configSections> | ||
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> | ||
</configSections> | ||
<log4net> | ||
<root> | ||
<level value="DEBUG" /> | ||
<appender-ref ref="RollingFileAppender" /> | ||
</root> | ||
<logger name="GlueInterop.Transport"> | ||
<level value="DEBUG" /> | ||
</logger> | ||
<appender name="OutputDebugStringAppender" type="log4net.Appender.OutputDebugStringAppender"> | ||
<layout type="log4net.Layout.PatternLayout"> | ||
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> | ||
</layout> | ||
</appender> | ||
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> | ||
<file type="log4net.Util.PatternString" value="logs\MultiWindowWPFDemo-%env{ComputerName}-%date{yyyyMMdd-HHmmss}.log" /> | ||
<appendToFile value="true" /> | ||
<maximumFileSize value="20MB" /> | ||
<maxSizeRollBackups value="10" /> | ||
<layout type="log4net.Layout.PatternLayout"> | ||
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> | ||
</layout> | ||
</appender> | ||
</log4net> | ||
</configuration> |
101 changes: 101 additions & 0 deletions
101
multiwindow-netcore-demo/MultiWindowWPFDemo/Log4NetLoggerFactory.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System; | ||
using System.Threading; | ||
using Glue.Logging; | ||
using log4net; | ||
using log4net.Core; | ||
|
||
namespace MultiWindowWPFDemo | ||
{ | ||
public class Log4NetLoggerFactory : IGlueLoggerFactory | ||
{ | ||
public IGlueLog GetLogger(string name) | ||
{ | ||
return new Log4NetGlueLog(LogManager.GetLogger(name)); | ||
} | ||
|
||
public IGlueLog GetLogger(Type type) | ||
{ | ||
return new Log4NetGlueLog(LogManager.GetLogger(type)); | ||
} | ||
|
||
public static Level GetLog4NetLevel(GlueLogLevel level) | ||
{ | ||
return level switch | ||
{ | ||
GlueLogLevel.All => Level.All, | ||
GlueLogLevel.Trace => Level.Trace, | ||
GlueLogLevel.Debug => Level.Debug, | ||
GlueLogLevel.Info => Level.Info, | ||
GlueLogLevel.Warn => Level.Warn, | ||
GlueLogLevel.Error => Level.Error, | ||
GlueLogLevel.Fatal => Level.Fatal, | ||
GlueLogLevel.Off => Level.Off, | ||
_ => throw new ArgumentOutOfRangeException(nameof(level), level, null) | ||
}; | ||
} | ||
|
||
public class Log4NetGlueLog : IGlueLog | ||
{ | ||
private readonly ILog log_; | ||
|
||
public Log4NetGlueLog(ILog log) | ||
{ | ||
log_ = log; | ||
} | ||
|
||
public void Trace(Func<string> messageCtor, Exception e = null) | ||
{ | ||
if (log_.Logger.IsEnabledFor(Level.Trace)) | ||
{ | ||
log_.Logger.Log(new LoggingEvent(new LoggingEventData | ||
{ | ||
Level = Level.Trace, | ||
Message = messageCtor(), | ||
LoggerName = log_.Logger.Name, | ||
ThreadName = Thread.CurrentThread.Name, | ||
TimeStampUtc = DateTime.UtcNow, | ||
ExceptionString = e?.ToString() | ||
})); | ||
} | ||
} | ||
|
||
public void Info(string message, Exception e = null) | ||
{ | ||
log_.Info(message, e); | ||
} | ||
|
||
public void Error(string message, Exception e = null) | ||
{ | ||
log_.Error(message, e); | ||
} | ||
|
||
public void Debug(string message, Exception e = null) | ||
{ | ||
log_.Debug(message, e); | ||
} | ||
|
||
public void Debug(Func<string> message, Exception e = null) | ||
{ | ||
if (log_.IsDebugEnabled) | ||
{ | ||
log_.Debug(message(), e); | ||
} | ||
} | ||
|
||
public bool IsEnabledFor(GlueLogLevel level) | ||
{ | ||
return log_.Logger.IsEnabledFor(GetLog4NetLevel(level)); | ||
} | ||
|
||
public void Warn(string message, Exception e = null) | ||
{ | ||
log_.Warn(message, e); | ||
} | ||
|
||
public void Log(GlueLogLevel level, string message, Exception exception) | ||
{ | ||
log_.Logger.Log(typeof(LogImpl), GetLog4NetLevel(level), message, exception); | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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