log4net logger logging unexpected levels based on config - wpf

I have been trying to understand the config of the log4net library and I think I have it except for some unexpected behavior.
I have a root logger that has a level set to INFO and another logger for a specific class that has level set to ERROR.
What I expected from this was that the class logger would only log at error and ignore the roots level since I had additivity set to false for the class logger. Here is the log4net.config I have at the moment:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<logger name="EveStatic.Config.ViewModel" additivity="false">
<level value="error"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="RollingFileAppender"/>
</logger>
<root>
<level value="debug" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
In my AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
And in the class that loads the configuration:
log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));
These two seem redundant but the configuration wont load unless I have the code part. Everything here is in a class library being used by another project.
Is this behavior expected and I don't understand the configuration and level overrides or am I forgetting something?
EDIT:
Here is how I instantiate and call the ILog. The the full class name is the name of the logger in the config plus the ConfiInfoViewModel:
private static readonly ILog LOG = LogManager.GetLogger(typeof(ConfigInfoViewModel));
...
LOG.Debug("Something buggy");
Also note that when testing the logging levels I had a log statement for each level in a series.

Your problem lays here
LogManager.GetLogger(typeof(ConfigInfoViewModel));
Internally this get resolved to
LogManager.GetLogger(typeof(ConfigInfoViewModel).FullName);
Now log4net is looking for a Logger named "EveStatic.Config.ConfigInfoViewModel" (result of typeof(ConfigInfoViewModel).FullName)
Because no Logger with that name is specified a new one with your default settings is used.
Also note that level specify a threshold, not a single level.
Example: level=warn means log warn an all levels above (error and fatal)

Related

WPF log4net is not writing to the log file [duplicate]

This question already has an answer here:
WPF-Log4Net used inside VSIX extension did not output log when installed at target VS
(1 answer)
Closed 4 years ago.
I have a WPF solution. I downloaded log4net dll, I added log4net.config and had set the "Copy to Output Directory" value as "Copy always".
log4net.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="myapp.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
And I added the below line in AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
then the below code in my TestWindowControl.xaml.cs
public partial class TestWindowControl : UserControl
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public TestWindowControl()
{
XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));
log.Info("info testing");
log.Debug("debug testing");
log.Error("error testing");
log.Fatal("fatal testing");
log.Warn("warn testing");
}
}
But logs are not writing to the file. It's working in Console application but not working for WPF. Am I missing something?
Try to set the filter inside appender
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<levelMax value="ERROR" />
</filter>

What is wrong with this log4.net configuration?

It sure looks like it should work. It throws no errors, steps through every line of code, but produces no log file. What is wrong here? It should produce a log file in the directory of the appender name, but no log file ever generates.
log4net.xml file:
<configuration>
<log4net debug="true">
<appender name ="task_appender" type="log4net.Appender.RollingAppender">
<file value="C:\Users\ryan\Documents\Visual Studio 2017\Workout Project\GPWorkouts\blablabla.txt"></file>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBacks value="2"/>
<maximumFileSize value="5000KB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline"></conversionPattern>
</layout>
</appender>
<root>
<level value="DEBUG"/>
</root>
<logger name="Task">
<level value="DEBUG"/>
<appender-ref ref="task_appender"/>
</logger>
</log4net>
</configuration>
application file:
public partial class MainWindow : Window
{
static protected ILog log = LogManager.GetLogger("Task");
static void log4net_demo()
{
FileInfo fi = new FileInfo("log4net.xml");
log4net.Config.XmlConfigurator.Configure(fi);
log4net.GlobalContext.Properties["host"] = Environment.MachineName;
}
public MainWindow()
{
InitializeComponent();
log4net_demo();
log.Info("This is the information log level");
log.Debug("This is the debugging log level");
log.Error("This is the error log level");
log.Fatal("This is the fatal log level");
}
Looks like some typo in your log4net config, the correct appender type name is log4net.Appender.RollingFileAppender
In your xml, it was given as log4net.Appender.RollingAppender
Here is the corrected xml
<configuration>
<log4net debug="true">
<appender name ="task_appender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Users\ryan\Documents\Visual Studio 2017\Workout Project\GPWorkouts\blablabla.txt"></file>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBacks value="2"/>
<maximumFileSize value="5000KB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline"></conversionPattern>
</layout>
</appender>
<root>
<level value="DEBUG"/>
</root>
<logger name="Task">
<level value="DEBUG"/>
<appender-ref ref="task_appender"/>
</logger>
</log4net>
</configuration>

When using Log4Net to log to database, field names appearing instead of values

I am having problems logging to my database using Log4Net.
I am not quite sure what to make of my results. I keep getting the field name when I should be getting the values of the fields.
The program is a SystemTray utility. I have set Log4Net up as follows.
In the Assembly:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
In the Program.cs:
private static readonly ILog log = LogManager.GetLogger(typeof(Program));
[STAThread]
static void Main()
{
log4net.Config.XmlConfigurator.Configure();
In the calling class:
public class HostManager
{
#region constants
protected static readonly ILog log = LogManager.GetLogger(typeof(HostManager));
...
// Inside an event method:
try
{
log.Info(new Log
{
CreateDate = DateTime.Now.ToString(),
SystemUpTime = FormatUpTime(_states.TimeFromStart),
EmotivType = "boredom",
EmotivValue = _states.AffectivEngagementBoredomScore.ToString(),
EventTimer = _eventTimer,
SubEventTimer = _subEventTimer
});
}
The Log class is just a bunch of getter and setters of type string.
And, my app.config file with the log4net settings:
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1"/>
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<connectionString value="Data Source=localhost; Initial Catalog=[MyDatabase]; Integrated Security=True;"/>
<commandText value="INSERT INTO Session ([CreateDate], [SystemUpTime], [EmotivType], [EmotivValue], [EventTimer], [SubEventTimer])
VALUES (#createdate, #systemuptime, #emotivtype, #emotivvalue, #eventtimer, #subeventtimer)"/>
<parameter>
<parameterName value="#createdate"/>
<dbType value="DateTime"/>
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="#systemuptime"/>
<dbType value="DateTime"/>
<layout type="log4net.Layout.RawTimeStampLayout"/>
</parameter>
<parameter>
<parameterName value="#emotivtype" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%emotivtype" />
</layout>
</parameter>
<parameter>
<parameterName value="#emotivvalue"/>
<dbType value="String"/>
<size value="255"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%emotivvalue"/>
</layout>
</parameter>
<parameter>
<parameterName value="#eventtimer"/>
<dbType value="DateTime"/>
<layout type="log4net.Layout.RawTimeStampLayout"/>
</parameter>
<parameter>
<parameterName value="#subeventtimer"/>
<dbType value="DateTime"/>
<layout type="log4net.Layout.RawTimeStampLayout"/>
</parameter>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="AdoNetAppender"/>
</root>
</log4net>
The result is that the %emotivtype and %emotivalue always receive the field names:
ID CreateDate SystemUpTime EmotivType EmotivValue EventTimer SubEventTimer
13547 17:06:56.787 17:06:56.787 emotivtype emotivvalue 17:06:56.787 17:06:56.787
This seems like such a silly, fixable thing but it escapes me. Any suggestions? Thanks.
log4net does not know how to deal with your Log class and thus simply call ToString() on it... I guess you have two options.
First Option:
You make your class return the fields you are interested in some format that you parse in stored procedure on the database. E.g.
emotivtype|emotivevalue|other fields...
then you configure a single parameter for the stored procedure (you need one to parse the above string in the database.
<parameter>
<parameterName value="#emotivvalue"/>
<dbType value="String"/>
<size value="255"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message"/>
</layout>
</parameter>
The obvious drawback is that you would not be able to get a decent output in a file or on a console. There are ways around this (custom layout converter) but I am not sure if this is the right approach.
Second Option:
You can use so called context properties (here is a good tutorial). In particular look at the "Calculated Context Values". I am not sure what I should write about this... maybe if you have specific questions about it I can answer them.

Impossible to create windows forms application using BusinessLogic of Nopcommerce v.1.9

My client ask me something who seems me very simple to do.
He has nopcommerce 1.9 web site , and he wish develop a simple windows forms application to modify clients adresses.
So i tryed to configure a news windows forms project :
static void Main(string[] args)
{
// Code that runs on application startup
NopConfig.Init();
//initialize IoC
IoC.InitializeWith(new DependencyResolverFactory());
//initialize task manager
TaskManager.Instance.Initialize(NopConfig.ScheduleTasks);
TaskManager.Instance.Start();
//open
new FormClient().Show();
TaskManager.Instance.Stop();
}
Then i create a service manager who expose data :
public class ServiceManager
{
public ICustomerService CustomerService;
public ServiceManager()
{
var dbContext = IoC.Resolve<NopObjectContext>();
CustomerService = new CustomerService(dbContext);
}
}
And impossible to access CustomerService methods because resolve method don't find the concrete class to instantiate for NopObjectContext;
(you can find nop commerce 1.9 in this location :
http://nopcommerce.codeplex.com/downloads/get/176949 )
Finally , it works :
App.config must be :
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="NopConfig" type="NopSolutions.NopCommerce.BusinessLogic.Configuration.NopConfig, Nop.BusinessLogic" requirePermission="false"/>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="NopSolutions.NopCommerce.BusinessLogic.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<appSettings>
<add key="dependencyResolverTypeName" value="NopSolutions.NopCommerce.BusinessLogic.Infrastructure.UnityDependencyResolver, Nop.BusinessLogic" />
</appSettings>
<connectionStrings>
<add name="NopEntities" connectionString="metadata=res://*/Data.NopModel.csdl|res://*/Data.NopModel.ssdl|res://*/Data.NopModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=EGG-BANKS;Initial Catalog=nopvierge2;Persist Security Info=True;User ID=sa;Password=sqlserver;MultipleActiveResultSets=True;Application Name=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="NopSqlConnection" connectionString="Data Source=DAVID-TOSH\SQLEXPRESS;Initial Catalog=nop;Integrated Security=True;Persist Security Info=False;MultipleActiveResultSets=True;Connect Timeout=120" />
</connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
<applicationSettings>
<NopSolutions.NopCommerce.BusinessLogic.Properties.Settings>
<setting name="Nop_BusinessLogic_Clickatell_PushServerWS" serializeAs="String">
<value>http://api.clickatell.com/soap/webservice_vs.php</value>
</setting>
<setting name="Nop_BusinessLogic_EuropaCheckVatService_checkVatService" serializeAs="String">
<value>http://ec.europa.eu/taxation_customs/vies/services/checkVatService</value>
</setting>
</NopSolutions.NopCommerce.BusinessLogic.Properties.Settings>
</applicationSettings>
<NopConfig>
<SqlServer ConnectionStringName="NopSqlConnection"/>
<ScheduleTasks>
<Thread seconds="60">
<!--do NOT enable ClearCache task if you have enabled tracking online users-->
<task name="ClearCache" type="NopSolutions.NopCommerce.BusinessLogic.Caching.ClearCacheTask, Nop.BusinessLogic" enabled="false" stopOnError="false"/>
<task name="PurgeOnlineUsers" type="NopSolutions.NopCommerce.BusinessLogic.Audit.UsersOnline.PurgeOnlineUsersTask, Nop.BusinessLogic" enabled="true" stopOnError="false"/>
<task name="Emails" type="NopSolutions.NopCommerce.BusinessLogic.Messages.SendQueuedMessagesTask, Nop.BusinessLogic" enabled="true" stopOnError="false" maxTries="5"/>
<task name="KeepAlive" type="NopSolutions.NopCommerce.BusinessLogic.Utils.KeepAliveTask, Nop.BusinessLogic" enabled="true" stopOnError="false" path="keepalive/ping.ashx"/>
</Thread>
<Thread seconds="600">
<task name="DeleteExpiredCustomerSessions" type="NopSolutions.NopCommerce.BusinessLogic.CustomerManagement.DeleteExpiredCustomerSessionsTask, Nop.BusinessLogic" enabled="true" stopOnError="false" deleteExpiredCustomerSessionsOlderThanMinutes="43200"/>
<task name="DeleteExpiredShoppingCarts" type="NopSolutions.NopCommerce.BusinessLogic.Orders.DeleteExpiredShoppingCartsTask, Nop.BusinessLogic" enabled="false" stopOnError="false" deleteExpiredShoppingCartsOlderThanMinutes="259200"/>
</Thread>
<Thread seconds="60">
<task name="UpdateExchangeRates" type="NopSolutions.NopCommerce.BusinessLogic.Directory.ExchangeRates.UpdateExchangeRateTask, Nop.BusinessLogic" enabled="true" stopOnError="false"/>
</Thread>
<Thread seconds="3600">
<task name="DatabaseMaintance" type="NopSolutions.NopCommerce.BusinessLogic.Maintenance.DatabaseMaintanceTask, Nop.BusinessLogic" enabled="false" stopOnError="false"/>
</Thread>
</ScheduleTasks>
</NopConfig>
</configuration>
with program.cs file :
static void Main(string[] args)
{
// Code that runs on application startup
NopConfig.Init();
//initialize IoC
IoC.InitializeWith(new DependencyResolverFactory());
//initialize task manager
TaskManager.Instance.Initialize(NopConfig.ScheduleTasks);
TaskManager.Instance.Start();
new form1().show();
TaskManager.Instance.Stop();
}

Castle Windsor error using log4net in WinForm app

I've this error using Windsor Castle with log4net in WinForm application.
Error:
Could not convert from 'Castle.Services.Logging.Log4netIntegration.Log4netFactory,Castle.Services.Logging.Log4netIntegration,Version=2.5.1.0, Culture=neutral,PublicKeyToken=407dd0808d44fbdc' to System.Type - Maybe type could not be found
Stack:
in Castle.MicroKernel.SubSystems.Conversion.TypeNameConverter.PerformConversion(String value, Type targetType) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\SubSystems\Conversion\TypeNameConverter.cs:riga 91
in Castle.MicroKernel.SubSystems.Conversion.DefaultConversionManager.PerformConversion(String value, Type targetType) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\SubSystems\Conversion\DefaultConversionManager.cs:riga 134
in Castle.MicroKernel.SubSystems.Conversion.DefaultConversionManager.PerformConversion[TTarget](String value) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\SubSystems\Conversion\DefaultConversionManager.cs:riga 162
in Castle.Facilities.Logging.LoggingFacility.GetLoggingFactoryType(LoggerImplementation loggerApi)
in Castle.Facilities.Logging.LoggingFacility.CreateProperLoggerFactory(LoggerImplementation loggerApi)
in Castle.Facilities.Logging.LoggingFacility.ReadConfigurationAndCreateLoggerFactory()
in Castle.Facilities.Logging.LoggingFacility.Init()
in Castle.MicroKernel.Facilities.AbstractFacility.Castle.MicroKernel.IFacility.Init(IKernel kernel, IConfiguration facilityConfig) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\Facilities\AbstractFacility.cs:riga 85
in Castle.MicroKernel.DefaultKernel.AddFacility(String key, IFacility facility) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\DefaultKernel.cs:riga 320
in Castle.MicroKernel.DefaultKernel.AddFacility[T](Func`2 onCreate) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\DefaultKernel.cs:riga 377
in Castle.Windsor.WindsorContainer.AddFacility[T](Func`2 onCreate) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Windsor\WindsorContainer.cs:riga 629
in IocWinFormTest.Installers.LoggerInstaller.Install(IWindsorContainer container, IConfigurationStore store) in C:\Sviluppo\IocWinFormTest\IocWinFormTest\IocWinFormTest\Installers\LoggerInstaller.cs:riga 18
in Castle.Windsor.WindsorContainer.Install(IWindsorInstaller[] installers, DefaultComponentInstaller scope) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Windsor\WindsorContainer.cs:riga 324
in Castle.Windsor.WindsorContainer.Install(IWindsorInstaller[] installers) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Windsor\WindsorContainer.cs:riga 674
in IocWinFormTest.Program.Main() in C:\Sviluppo\IocWinFormTest\IocWinFormTest\IocWinFormTest\Program.cs:riga 22
in System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
in System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
in System.Threading.ThreadHelper.ThreadStart_Context(Object state)
in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
in System.Threading.ThreadHelper.ThreadStart()
the code:
public class LoggerInstaller : IWindsorInstaller
{
#region IWindsorInstaller Members
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.Log4net).WithAppConfig());
}
#endregion
}
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<logger name="NHibernate">
<level value="WARN" />
</logger>
<logger name="NHibernate.SQL">
<level value="ALL" />
<appender-ref ref="RollingFile" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFile" />
<appender-ref ref="trace" />
</root>
<appender name="trace" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value=" %date %level %message%newline" />
</layout>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Public\Log" />
<appendToFile value="true" />
<datePattern value=".yyyyMMdd.\tx\t" />
<rollingStyle value="Date" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d %-8ndc %-5thread %-5level %logger %message %timestampms %n" />
</layout>
</appender>
</log4net>
</configuration>
I used NuGet to import all packages and interesting story is that I've the same configuration in a web app and there it works!
Can you help me?
Daniele
Have you copied the Castle.Services.Logging.Log4netIntegration.dll and all other required assemblies to your project's folder?
Change the target of the WinForm app from the Client Profile to the .Net Framework. log4net depends on System.Web as a support assembly. The answer to my question has more details if you want to stay targeting the Client Profile.
Why does log4net 1.2.10 require System.Web?

Resources