log file for an WPF application [Resolved]

Posted by Sudhakar_A under C# on 9/18/2013 | Points: 10 | Views : 6814 | Status : [Member] | Replies : 6
I wan to create an log which keep track of all the event i.e intially i send packet to ma device and it response back and catch any error occurs in the project.


pls help




Responses

Posted by: Bandi on: 9/19/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Refer these links
http://stackoverflow.com/questions/4474206/create-log-file-and-update-status-dynamically-in-c-net
http://it.toolbox.com/blogs/daniel-at-work/using-log4net-in-cnet-26794
http://www.codeproject.com/Articles/140911/log4net-Tutorial

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/18/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer these links
http://www.codeproject.com/Articles/43027/Logging-display-and-WPF
http://www.codeproject.com/Articles/39218/How-To-Create-a-Windows-Event-Log-and-Write-your-C
http://stackoverflow.com/questions/16743804/implementing-a-log-viewer-with-wpf
http://blogs.msdn.com/b/bclteam/archive/2005/03/15/396431.aspx
http://stackoverflow.com/questions/10746670/how-to-monitor-that-an-application-is-opened

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/18/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
You can use the __InstanceCreationEvent event and the Win32_Process WMI class to monitor the created processes.

Try this sample C# application
using System;

using System.Collections.Generic;
using System.Management;
using System.Text;


namespace GetWMI_Info
{
public class EventWatcherAsync
{
private void WmiEventHandler(object sender, EventArrivedEventArgs e)
{
//in this point the new events arrives
//you can access to any property of the Win32_Process class
Console.WriteLine("TargetInstance.Handle : " + ((ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value)["Handle"]);
Console.WriteLine("TargetInstance.Name : " + ((ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value)["Name"]);

}

public EventWatcherAsync()
{
try
{
string ComputerName = "localhost";
string WmiQuery;
ManagementEventWatcher Watcher;
ManagementScope Scope;

Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);
Scope.Connect();

WmiQuery ="Select * From __InstanceCreationEvent Within 1 "+
"Where TargetInstance ISA 'Win32_Process' ";

Watcher = new ManagementEventWatcher(Scope, new EventQuery(WmiQuery));
Watcher.EventArrived += new EventArrivedEventHandler(this.WmiEventHandler);
Watcher.Start();
Console.Read();
Watcher.Stop();
}
catch (Exception e)
{
Console.WriteLine("Exception {0} Trace {1}", e.Message, e.StackTrace);
}

}

public static void Main(string[] args)
{
Console.WriteLine("Listening process creation, Press Enter to exit");
EventWatcherAsync eventWatcher = new EventWatcherAsync();
Console.Read();
}
}
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudhakar_A on: 9/19/2013 [Member] Starter | Points: 25

Up
0
Down
chandu@ Thnk's for u r Answer.................But i wan to do it using references log4net. Can u pls help me and step by step procedure.

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer these links for WPF logging using log4net
http://www.codeproject.com/Tips/381212/log4Net-logging-framework
http://hirenkhirsaria.blogspot.in/2012/06/logging-using-log4net-in-wpf.html
http://www.codeproject.com/Articles/30795/C-WPF-Log4Net-Viewer
http://www.codeproject.com/Articles/43027/Logging-display-and-WPF

http://stackoverflow.com/questions/18695573/log4net-doesnt-log-when-deployed

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
How to configure, initialize and using log4net in WPF app
Using log4net in MVC (Model View Control) Architecture.
creating an class file let us name class file as logger4net.cs
Placing logger4net in control package

The code inside logger4net .cs
using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
namespace WpfApplication1.App1.control
{
public class logger4net
{
protected static ILog log;
public static ILog Log
{
get
{
if (log == null)
{
log = log4net.LogManager.GetLogger(typeof(logger4net));
}
return log;
}
}
}


create another class file name as App_Exception.cs
using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.ActiveRecord.Framework;
using NHibernate;
namespace WpfApplication1.App1.control
{
public class App_Exception : Exception
{
public App_Exception(string message)
: base(message)
{
logger4net.Log.Error(Message);
logger4net.Log.Error(StackTrace);
}
public App_Exception(Object exceptionObject)
{
string stackTrace = “”;
string message = “”;

if (exceptionObject is ActiveRecordException)
{
ActiveRecordException aex = (ActiveRecordException)exceptionObject;
message += aex.Message + “\n”;
stackTrace += aex.StackTrace + “\n”;
Exception iex = aex.InnerException;
while (iex != null)
{
message += iex.Message + “\n”;
iex = iex.InnerException;
}
}
else if (exceptionObject is HibernateException)
{
HibernateException aex = (HibernateException)exceptionObject;
message += aex.Message + “\n”;
stackTrace += aex.StackTrace + “\n”;
Exception iex = aex.InnerException;
while (iex != null)
{
message += iex.Message + “\n”;
iex = iex.InnerException;
}
}
else if (exceptionObject is Exception)
{
Exception aex = (Exception)exceptionObject;
message += aex.Message + “\n”;
stackTrace += aex.StackTrace + “\n”;
}
logger4net.Log.Error(message);
logger4net.Log.Error(stackTrace);
throw new Exception(message);
}
}
}
Now use

try{ }
catch(Exception ex)
{
logger4net.Log.Error(ex);
throw new App_Exception(ex);
}

The above code explains you that when we throw exception only outer exception is thrown For exact exception all exception should be thrown such as inner exception.

Configuring app.config file.
Place following code inside the app.config file:

<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<!--File value=log\logfile.txt-->
<param name="File" value="null"/>
<appendToFile value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
</layout>
</appender>
<appender name="ServerFileAppender" type="log4net.Appender.RollingFileAppender">
<!--<file value="log\log-file.txt" />-->
<param name="File" value="YourLogName.log"/>
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
</layout>
</appender>
<root>
<level value="ERROR" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="FileAppender"/>
<appender-ref ref="ServerFileAppender"/>
</root>
</log4net>
Now add Initilising code in app.xaml.cs file:

public bool ChangeLogFileName(string AppenderName, string NewFilename)
{
log4net.Repository.ILoggerRepository RootRep;
RootRep = log4net.LogManager.GetRepository();

foreach (log4net.Appender.IAppender iApp in RootRep.GetAppenders())
{
if (iApp.Name.CompareTo(AppenderName) == 0 && iApp is log4net.Appender.FileAppender)
{

log4net.Appender.FileAppender fApp = (log4net.Appender.FileAppender)iApp;

fApp.File = NewFilename;
fApp.ActivateOptions();
return true; // Appender found and name changed to NewFilename
}
}

return false; // appender not found
}
public void IntializeLog4net()
{
try
{

log4net.Config.XmlConfigurator.Configure();

string Logfile = "log\\" + "Filename_" + DateTime.Now.ToString("LogfileLocation") + ".log";
ChangeLogFileName("FileAppender", Logfile);

}
catch (Exception ex)
{
throw ex;
}
}

public App() /* Default constructor */
{ IntializeLog4net(); }

Now use the following line for logging the errors... logger4net.Log.Error(ex);
Or we can use try catch block for throwing entire exceptions...

try{ }
catch(Exception ex)
{

throw new App_Exception(ex);
}


Creating app.config file shown in the link: http://codetechnics.blogspot.com/2010/09/how-to-create-appconfig-in-wpf.html

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response