Can't create a logger due to out of date example code and documentation.
JasonAtDrumBrae opened this issue · 1 comments
I would love to see some decent examples which reflect how Castle works today, not 10 years ago. So much of the documentation is totally out of date, describing the use of deprecated API's. I just started looking at Castle logging as Log4Net is not getting updates and I want to be able to move easily, probably to Serilog but lets leave it open. So I'm looking for a façade. We have purely desktop applications, a mixture of client apps, tools and NUnit3 based test frameworks. My classes all have the
ILog logger = LogManager.GetLogger(typeof(MyClass))
so how can I replace that easily. Looking at the documentation out there, and not being a castle expert, I've mainly used NInject to date, I've no idea.
public class LoggerInstaller : IWindsorInstaller
{
/// <summary>
/// Install the Windsor Logger.
/// </summary>
/// <param name="clsContainer">The Windsor container interface.</param>
/// <param name="clsStore">The configuration store interface.</param>
public void Install(IWindsorContainer clsContainer, IConfigurationStore clsStore)
{
clsContainer.AddFacility<LoggingFacility>(
cls => cls.LogUsing<Log4NetLoggerFactory>().WithConfig(@".\Logging\Log4Net.config"));
//f => f.UseAbpLog4Net().WithConfig(@".\Logging\Log4Net.config"));
}
}
Created myself a logger installer.
Tried to create a logger but its not happening and I have no idea how to resolve it yet.
using ILogger = Castle.Core.Logging.ILogger;
/// <summary>
/// The fixture manager.
/// </summary>
[SetUpFixture]
public class SetUpFixture
{
private static readonly WindsorContainer r_clsWindsorContainer = new();
static SetUpFixture()
{
r_clsWindsorContainer.Install(new LoggerInstaller());
}
/// <summary>
/// The test suite log.
/// </summary>
private readonly ILogger m_clsLog = NullLogger.Instance;
Log4Net file looks like this
<?xml version="1.0"?>
<log4net>
<appender name="Console" type="log4net.Appender.ManagedColoredConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<conversionPattern value="%file(%line): %level: %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>
</appender>
<appender name="Memory" type="log4net.Appender.MemoryAppender">
<onlyFixPartialEventData value="true" />
<layout type="log4net.Layout.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<conversionPattern value="%file:%line - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>
</appender>
<appender name="Trace" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<conversionPattern value="%file(%line): %level: %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="AllegroMicroLog.html" />
<appendToFile value="true" />
<maximumFileSize value="1000KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<converter>
<name value="filename" />
<type value="TestCastleWindsor.Logging.FileNamePatternConverter" />
</converter>
<converter>
<name value="HtmlColorPrefix" />
<type value="TestCastleWindsor.Logging.HtmlLogColorPrefixConverter" />
</converter>
<converter>
<name value="HtmlPrefix" />
<type value="TestCastleWindsor.Logging.HtmlLogPrefixConverter" />
</converter>
<converter>
<name value="HtmlSuffix" />
<type value="TestCastleWindsor.Logging.HtmlLogSuffixConverter" />
</converter>
<!-- <conversionPattern value="%HtmlColorPrefix [%-5level] %HtmlSuffix %HtmlPrefix %date{dd MMM yyyy HH:mm:ss} %filename:%line - %message %HtmlSuffix %newline" /> -->
<conversionPattern value="[%-5level] %file:%line - %message % %newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="Console" />
<appender-ref ref="Memory" />
<appender-ref ref="RollingFile" />
<appender-ref ref="Trace" />
</root>
</log4net>
The question is, how do I create a logger? I want one that follows the config file and is able to log to the console, file etc.
The following will write to the console but then I can only do one type of logging, an what about the configuration I asked for?
[ILoggerFactory clsFactory = new ConsoleFactory();
m_clsLog = clsFactory.Create(typeof(TestLogging));
Assert.That(m_clsLog.IsInfoEnabled, Is.True);
I would love to see some decent examples which reflect how Castle works today, not 10 years ago. So much of the documentation is totally out of date, describing the use of deprecated API's.
Things haven't changed much in 10 years, the XML configuration is still there if people want to use it or are supporting existing software, but most will use the fluent C# API. If there is documentation that's out of date, feel free to submit a pull request or describe it in an issue.
Created myself a logger installer.
Tried to create a logger but its not happening and I have no idea how to resolve it yet.
The first paragraph of the logging facility documentation explains that you can either inject an ILogger
into your class, which you can't do with your SetUpFixture
class as NUnit will instantiate your object, or you can explicitly resolve either an ILogger
or ILoggerFactory
when Windsor isn't managing the lifecycle of the object.