Search This Blog
Monday, September 17, 2012
NUnit AutoFixture unable to create an instance from IList
I encountered a problem with creating fake fixture objects in .NET tests for classes which contant a property with IList<T> (instead plain List<T>).
Suppose you have:
public class A
{
public IList<B> {
get { return list; }
set { list = value; }
}
private IList<B> list;
}
and you want to create a fake object like this:
var fake = new Fixture().Build<A>();
You'll get an error:
"AutoFixture was unable to create an instance from System.Collections.Generic.IList`1[A], most likely because it has no public constructor, is an abstract or non-public type."
What you need to do is tell AutoFixture what to use when it encounters an IList inferface:
(when there is an IList create a new List of some type):
var fixture = new Fixture();
fixture.Register<IList<A>>(fixture.CreateAnonymous<List<A>>);
fixture.Build<A>();
Thursday, September 13, 2012
log4net nhibernate stop logging to console
After using log4net and nhibernate in one project I wanted to log sql commands in a separate file. I didn't want these sql commands to appear on console either. My castle implementation of nhibernate logged either all to console and separate file or nothing.
That's the configuration I used to stop nhibenrate from logging to console and force it to log to a separate file:
download
The important part is to use a filter inside a ConsoleAppender:
<filter type="log4net.Filter.LoggerMatchFilter">
<LoggerToMatch value="NHibernate"/>
<acceptOnMatch value="false"/>
</filter>
That's the configuration I used to stop nhibenrate from logging to console and force it to log to a separate file:
download
The important part is to use a filter inside a ConsoleAppender:
<filter type="log4net.Filter.LoggerMatchFilter">
<LoggerToMatch value="NHibernate"/>
<acceptOnMatch value="false"/>
</filter>
Tuesday, September 11, 2012
log4net quickstart; log4net FileAppender not working in ConsoleApplication
To quickly set up log4net logging in you application open you app.config file:
Add a section for log4net:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" requirePermission="false"/>
</configSections>
then add the section itself:
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="RollingLogFileAppender" />
</root>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{HH:mm:ss,fff} - %message%newline" />
</layout>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="c:\a\xx.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="500MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
</log4net>
In your code do the following:
XmlConfigurator.Configure();
ILog logger = LogManager.GetLogger(typeof(Program));
logger.Info("a");
WARNING:
Do not use: BasicConfigurator.Configure(); as it does not work with this example.
Normally you'd declare a private variable for logging:
private static readonly ILog logger = LogManager.GetLogger(typeof (Program));
Add a section for log4net:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" requirePermission="false"/>
</configSections>
then add the section itself:
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="RollingLogFileAppender" />
</root>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{HH:mm:ss,fff} - %message%newline" />
</layout>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="c:\a\xx.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="500MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
</log4net>
In your code do the following:
XmlConfigurator.Configure();
ILog logger = LogManager.GetLogger(typeof(Program));
logger.Info("a");
WARNING:
Do not use: BasicConfigurator.Configure(); as it does not work with this example.
Normally you'd declare a private variable for logging:
private static readonly ILog logger = LogManager.GetLogger(typeof (Program));
Subscribe to:
Posts (Atom)