In order to make more complex logs, I've decided to use the Log4Net for my WPF desktop app. It works perfectly, creating both 'Debug' messages at the 'Output' and writes to a file all the logs...
BUT if I deploy it/install it (using visual studio's wizard to create a .MSI setup) it stops working. I can't find any file on the folder or anywhere on the computer related to it.
I'm using Caliburn.Micro to organize my code. And in order to have the logs, I 'centralized' it by using a public static class by the name of "Logging.cs".
The code inside "Initialize()" was put right now as a way to try out, but it doesn't seem to be doing anything.
public static class Logging
{
//Declaration of the logger
/// <summary>
/// Logger for the Logging manager.
/// </summary>
private static log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void Initialize()
{
log4net.Config.XmlConfigurator.Configure();
}
/// <summary>
/// Creates a log in Fatal level accompanied by an expection
/// </summary>
/// <param name="message">The message to be logged</param>
/// <param name="error">Exception thrown along with the log</param>
/// <param name="senderType">From where the log comes from</param>
public static void Fatal(string message, Exception error, Type senderType)
{
//Defines 'type' in the log.
Logger = log4net.LogManager.GetLogger(senderType);
Logger.Fatal(message, error);
}
/// <summary>
/// Creates a log in Fatal level
/// </summary>
/// <param name="message">The message to be logged</param>
/// <param name="senderType">From where the log comes from</param>
public static void Fatal(string message, Type senderType)
{
//Defines 'type' in the log.
Logger = log4net.LogManager.GetLogger(senderType);
Logger.Fatal(message);
}
/// <summary>
/// Creates a log in Error level accompanied by an expection
/// </summary>
/// <param name="message">The message to be logged</param>
/// <param name="error">Exception thrown along with the log</param>
/// <param name="senderType">From where the log comes from</param>
public static void Error(string message, Exception error, Type senderType)
{
//Defines 'type' in the log.
MessageBox.Show(error.ToString());
Logger = log4net.LogManager.GetLogger(senderType);
Logger.Error(message, error);
}
/// <summary>
/// Creates a log in Error level
/// </summary>
/// <param name="message">The message to be logged</param>
/// <param name="senderType">From where the log comes from</param>
public static void Error(string message, Type senderType)
{
//Defines 'type' in the log.
Logger = log4net.LogManager.GetLogger(senderType);
Logger.Error(message);
}
/// <summary>
/// Creates a log in Warn level accompanied by an expection
/// </summary>
/// <param name="message">The message to be logged</param>
/// <param name="error">Exception thrown along with the log</param>
/// <param name="senderType">From where the log comes from</param>
public static void Warn(string message, Exception error, Type senderType)
{
//Defines 'type' in the log.
Logger = log4net.LogManager.GetLogger(senderType);
Logger.Warn(message, error);
}
/// <summary>
/// Creates a log in Warn level
/// </summary>
/// <param name="message">The message to be logged</param>
/// <param name="senderType">From where the log comes from</param>
public static void Warn(string message, Type senderType)
{
//Defines 'type' in the log.
Logger = log4net.LogManager.GetLogger(senderType);
Logger.Warn(message);
}
/// <summary>
/// Creates a log in Info level accompanied by an expection
/// </summary>
/// <param name="message">The message to be logged</param>
/// <param name="error">Exception thrown along with the log</param>
/// <param name="senderType">From where the log comes from</param>
public static void Info(string message, Exception error, Type senderType)
{
//Defines 'type' in the log.
Logger = log4net.LogManager.GetLogger(senderType);
Logger.Info(message, error);
}
/// <summary>
/// Creates a log in Info level
/// </summary>
/// <param name="message">The message to be logged</param>
/// <param name="senderType">From where the log comes from</param>
public static void Info(string message, Type senderType)
{
//Defines 'type' in the log.
Logger = log4net.LogManager.GetLogger(senderType);
Logger.Info(message);
}
/// <summary>
/// Creates a log in Debug level accompanied by an expection
/// </summary>
/// <param name="message">The message to be logged</param>
/// <param name="error">Exception thrown along with the log</param>
/// <param name="senderType">From where the log comes from</param>
public static void Debug(string message, Exception error, Type senderType)
{
//Defines 'type' in the log.
Logger = log4net.LogManager.GetLogger(senderType);
Logger.Debug(message, error);
}
/// <summary>
/// Creates a log in Debug level
/// </summary>
/// <param name="message">The message to be logged</param>
/// <param name="senderType">From where the log comes from</param>
public static void Debug(string message, Type senderType)
{
//Defines 'type' in the log.
Logger = log4net.LogManager.GetLogger(senderType);
Logger.Debug(message);
}
/// <summary>
/// Log created in Debug level containing not only a log message but also an object to be serialized and logged.
/// </summary>
/// <param name="message">The message to be logged (should be description of object)</param>
/// <param name="obj">Object to be displayed</param>
/// <param name="senderType">From where the log comes from</param>
public static void ShowObject(string message, object obj, Type senderType)
{
//Defines 'type' in the log.
Logger = log4net.LogManager.GetLogger(senderType);
Logger.Debug(message + "\n<object>" + JsonConvert.SerializeObject(obj) + "</object>\n");
}
}
Also, here is my log4net config file:
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="console" />
<appender-ref ref="file" />
</root>
<appender name="console" type="log4net.Appender.ColoredConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - [%message] // %exception%newline" />
</layout>
</appender>
<appender name="file" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="DesktopApp.log" />
<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] // %exception%newline" />
</layout>
</appender>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
</log4net>
And at the AssemblyInfo.cs, I've merely inserted this at the end:
[assembly: log4net.Config.XmlConfigurator(Watch = true,ConfigFile ="log4net.config")]
Thank you for your time, please comment if I can add any more details.
P.S.: The software is installed inside the AppData, so no file permission issues should arise, since I am using another framework to automatically update the app.
P.S.2: I used DebugView and in the middle of mostly info that seemed random I've found an error:
[5480] log4net:ERROR Failed to find configuration section 'log4net' in the application's .config file. Check your .config file for the <log4net> and <configSections> elements. The configuration section should look like: <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
Still haven't tried fixing it, just keeping this question up to date. (Thanks Varun!)