Nlog doesn't print messages on application close - wpf

All ViewModel classes inherit from a base class:
public abstract class ScreenBase : ViewModelBase, IScreen, IDisposable
{
protected readonly NLog.Logger _logger;
protected ScreenBase()
: this(Messenger.Default) { }
protected ScreenBase(IMessenger messenger)
: base(messenger)
{
_logger = NLog.LogManager.GetLogger(this.GetType().Name);
_logger.Debug("{0} ({1}) constructed. ", this.GetType().Name, this.GetHashCode());
}
~ScreenBase()
{
FinalizeProc();
}
[Conditional("DEBUG")]
private void FinalizeProc()
{
_logger.Debug("{0} ({1}) Finalized. ", this.GetType().Name, this.GetHashCode());
}
}
As you can see, any time instance of ViewModel is created / destroyed, it should log it. I am logging it into Console window and file:
<targets>
<!-- add your targets here -->
<target name="logDebugInfo" xsi:type="File" deleteOldFileOnStartup="true" fileName="${specialfolder:folder=CommonApplicationData}/MyApp/debug.txt" layout="${longdate} | ${uppercase:${level}} | ${stacktrace} | ${message}${onexception:EXCEPTION OCCURRED\:${exception:format=tostring}}" />
<target name="console" xsi:type="Console" />
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="Trace" maxlevel="Info" writeTo="logDebugInfo" />
<logger name="*" minlevel="Trace" writeTo="console" />
</rules>
Each message is properly logged until the application is closed. When 'X' Close button is pressed on the main view, I do not do anything specific in code, I let the application close itself. However, no 'Finalized' messages are displayed at that point.
Anyone knows why?

The reason why this is happening is that Logger is being destroyed on application shutdown.

Related

Why does this static Receiver not respond to the ACTION_POWER_(DIS)CONNECTED Event?

I want a STATIC Broadcast Receiver to get fired, whenever the Phone gets charged or not. In my Manifest.xml, i have written the following Entry:
<receiver
android:name=".Starter">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
The Broadcast Receiver itself looks like this:
public class Starter extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "blablabla", Toast.LENGTH_LONG).show();
}
}
When I register the Receiver programmatically, everything works fine, but I want the Receiver also to respond, when the Activity is not open, so I have to make a static Receiver.
Thank you for your help!!!

unable to create log file

I am using below code for creating logging in my project using PRISM ILoggrFacade.
But i am not sure is the best way and second thing how can i use CustomLogger class logging because i am unable to create logging.
public class CustomLogger : ILoggerFacade
{
protected static readonly ILog log = LogManager.GetLogger(typeof(CustomLogger));
public CustomLogger()
{
log4net.Config.XmlConfigurator.Configure();
}
public void Log(string message, Category category, Priority priority)
{
switch (category)
{
case Category.Debug:
log.Debug(message);
break;
case Category.Warn:
log.Warn(message);
break;
case Category.Exception:
log.Error(message);
break;
case Category.Info:
log.Info(message);
break;
}
}
}
Bootstrapper
private readonly CustomLogger _logger = new CustomLogger();
protected override ILoggerFacade CreateLogger()
{
return _logger;
}
ViewModel
logger.Log("Logging success", Category.Info, Priority.None);
log4net.config
<?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="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="C:\temp\log.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
Problem with above code is that need to configure log4net in assembly like below:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4Net.config", Watch = true)]
Also in addition if you wan to drag log4net in ViewModels the use below code
private readonly ILoggerFacade _loggerFacade;
public ContentAViewViewModel(ILoggerFacade loggerFacade)
{
_loggerFacade = loggerFacade;
}
Example
private void LoadPeople()
{
_loggerFacade.Log("Load People", Category.Debug, Priority.High);
}

How to keep the Shell configurable with PRISM 4.1 & Unity?

I am writing a WPF application using PRISM 4.1 & Unity. The application will have every UI component configurable, including the Shell itself!
E.g. I have an interface called IShell. Consumers of the app can have their own implementation of IShell if they are not satisfied with my default implementation which has Regions and Views defined in some fixed way.
Now from my Bootstrapper class (which inherits UnityBootstrapper), I want to know the type registered for IShell with the Unity container. The overriden CreateShell will return configured type of the IShell.
My App.config looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity>
<container>
<register type="Interfaces.IShell, Interfaces" mapTo="PrismApp.Shell, PrismApp"/>
</container>
</unity>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
In Bootstrapper class, I have following code:
public class PrismAppBootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
var obj = ServiceLocator.Current.GetInstance<IShell>() as DependencyObject;
return obj;
}
}
In the WPF application's App.xaml.cs, I am instantiating PrismAppBootstrapper:
PrismAppBootstrapper prismAppBootstrapper = new PrismAppBootstrapper();
prismAppBootstrapper.Run();
However, when I run this code, I get the Exception : "InvalidOperationException - The current type, Interfaces.IShell, is an interface and cannot be constructed. Are you missing a type mapping?"
How to solve this problem?
Why does application fail to know the registered type of IShell which is there in app.config file?
Keeping the Shell configurable is possible with the Unity container.
The problem was that my Unity Container was not properly configured to read mapping/registrations from the app.config file. So it failed to know the mapping of IShell at run time.
I had to override other methods along with CreateShell in my PrismAppBootstrapper class.
E.g.
public class PrismAppBootstrapper : UnityBootstrapper
{
protected override IModuleCatalog CreateModuleCatalog()
{
ModuleCatalog catalog = new ConfigurationModuleCatalog();
return catalog;
}
protected override void ConfigureContainer()
{
base.ConfigureContainer();
UnityConfigurationSection configurationSection =
(UnityConfigurationSection) ConfigurationManager.GetSection("unity");
if (configurationSection != null)
{
configurationSection.Configure(this.Container);
}
}
protected override DependencyObject CreateShell()
{
IShell shell = this.Container.TryResolve<IShell>();
return shell as Window;
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Window)this.Shell;
Application.Current.MainWindow.Show();
}
}
Shouldnt you instead use the UnityBootstrapper's Container to resolve your shell?
var obj = Container.Resolve<IShell>() as DependencyObject;
return obj;

NLog MethodCall

I've got a silverlight project and I'm trying to configure NLog for calling static method but it doesn't (using Nlog.config).
I'm following this example.
Here's Nlog.config code:
...
<targets>
<target name="m" xsi:type="MethodCall"
className="NLogTestSilver.MainPage, NLogTestSilver"
methodName="LogMethod">
<parameter layout="${level}" />
<parameter layout="${message}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="m" />
</rules>
...
Assembly name = NLogTestSilver.dll
Here's MainPage.xaml.cs code:
namespace NLogTestSilver
{
public partial class MainPage : UserControl
{
public static Logger Logger = LogManager.GetCurrentClassLogger();
public MainPage()
{
InitializeComponent();
Logger.Fatal("Fatality");
}
public static void LogMethod(string level, string message)
{
System.Windows.Browser.HtmlPage.Window.Alert(level + " " + message);
}
}
}
P.S. Programmatic configuration works well.
So it was found out that an exception is thrown in NLog.Targets.MethodCallTarget.InitializeTarget() method while processing className parameter.
If we change
className="NLogTestSilver.MainPage, NLogTestSilver"
to
className="NLogTestSilver.MainPage, NLogTestSilver,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx"
it works great.
Where xxxxxxxxxxxxxxxx is public key token of our assembley.

log4net configuration for Event Logging

I'm trying to enable solution-wide logging by adding a stand-alone project that wraps log4net. I found a code on StackOverflow but the code is using some config file. I do not understand that bit. Here is the only static class:
using log4net;
using log4net.Config;
using System;
using System.IO;
namespace ExciteEngine2.LoggingManager {
//// TODO: Implement the additional GetLogger method signatures and log4net.LogManager methods that are not seen below.
public static class ExciteLog {
private static readonly string LOG_CONFIG_FILE = #"log4net.config";
public static ILog GetLogger(Type type) {
// If no loggers have been created, load our own.
if (LogManager.GetCurrentLoggers().Length == 0) {
LoadConfig();
}
return LogManager.GetLogger(type);
}
private static void LoadConfig() {
//// TODO: Do exception handling for File access issues and supply sane defaults if it's unavailable.
try {
XmlConfigurator.ConfigureAndWatch(new FileInfo(LOG_CONFIG_FILE));
}
catch (Exception ex) {
}
}
}
}
Now, there is no log4net.config anywhere. And in my main application project, I'm using the ILog as follows:
using log4net;
using ExciteEngine2.LoggingManager;
namespace ExciteEngine2.MainApplication {
internal static class Program {
public static readonly ILog ApplicationLogger = ExciteLog.GetLogger(typeof(Program));
private static void SetupLogging() {
log4net.Config.XmlConfigurator.Configure();
}
[STAThread] static void Main(string[] args) {
//Uninstall
foreach (string arg in args) {
if (arg.Split('=')[0] == "/u") {
Process.Start(new ProcessStartInfo(Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\msiexec.exe", "/x " + arg.Split('=')[1]));
return;
}
}
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
try {
ThemeResolutionService.ApplicationThemeName = ConfigurationManager.AppSettings["ThemeToUse"];
}
catch (Exception ex) {
ApplicationLogger.Error("Exception while setting Telerik Theme.", ex);
ThemeResolutionService.ApplicationThemeName = "ControlDefault";
}
DevExpress.UserSkins.OfficeSkins.Register();
DevExpress.UserSkins.BonusSkins.Register();
DevExpress.Skins.SkinManager.EnableFormSkins();
DevExpress.Skins.SkinManager.EnableMdiFormSkins();
//try {
if (args.Contains("/dx")) {
Application.Run(new AppMDIRibbonDX());
ApplicationLogger.Info("Application (DX) started.");
}
else {
Application.Run(new AppMDIRibbon());
ApplicationLogger.Info("Application started.");
}
//} catch (Exception ex) {
// ApplicationLogger.Fatal("Exception while initiating. Nothing can be done here.", ex);
// XtraMessageBox.Show(String.Format("Exception while initiating. Nothing can be done here.{0}Message: {1}", Environment.NewLine, ex.Message), "Excite Engine 2", MessageBoxButtons.OK, MessageBoxIcon.Error);
//}
}
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) {
ApplicationLogger.Fatal("Application Level Exception.", e.Exception);
Thread t = (Thread)sender;
Exception threadexception = e.Exception;
string errormessage = String.Format("Thread ID: {0} [ {1} ]", t.ManagedThreadId, threadexception.Message);
XtraMessageBox.Show(String.Format("Application Level Exception!{1}{0}{1}Details:{1}{2}", errormessage, Environment.NewLine, threadexception.StackTrace), "Excite Engine 2", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
As you can see from my flow, I'm executing this line of code thinking log4net will use my main application project's app.config: log4net.Config.XmlConfigurator.Configure();
And here is a line I added in AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Finally, the app.config for my Main application:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<appSettings>
</appSettings>
<connectionStrings>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="Excite Engine 2 Log.log" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{ddd, dd-MMM-yyyy hh:mm:ss} - %m%n" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<applicationName value="Excite Engine 2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
When I run with <appender-ref ref="LogFileAppender" />, I get an empty file named Excite Engine 2 Log.log right next to my main EXE. And when I set <appender-ref ref="EventLogAppender" />, nothing happens in Event Viewer. Also, there is an attribute: <level value="DEBUG" /> thats really bothering me. What I want is a full EventViewer logging for my application regardless of the build configuration it is running in.
Appreciate if someone could guide me on this. Thanks!
I found a code on StackOverflow but
the code is using some config file. I
do not understand that bit.
The reason he's using a specific config file can be explained by the following taken from log4net's site:
The System.Configuration API is only
available if the configuration data is
in the application's config file; the
file named MyApp.exe.config or
Web.config. Because the
System.Configuration API does not
support reloading of the config file
the configuration settings cannot be
watched using the
log4net.Config.XmlConfigurator.ConfigureAndWatch
methods. The main advantage of using
the System.Configuration APIs to read
the configuration data is that it
requires less permissions than
accessing the configuration file
directly. The only way to configure an
application using the
System.Configuration APIs is to call
the
log4net.Config.XmlConfigurator.Configure()
method or the
log4net.Config.XmlConfigurator.Configure(ILoggerRepository)
method.
Edit:
To log to your log file you need to call your SetupLogging method above.
log4net.Config.XmlConfigurator.Configure();
This statement is never being called. It looks like you are calling LoadConfig() in your ExciteEngine2.LoggingManager but this uses a config file called log4net.config which you said doesn't exist. If you are putting your configuration in your app.config file then you need to call your SetupLogging method.

Resources