Solved

How effectively I could debug .NET Windows applications and applications with MVVM pattern

Posted on 2013-01-04
7
465 Views
Last Modified: 2013-01-21
I've experience in ASP.NET. Now I'm working in .NET windows application. How effectively I could debug .NET Windows applications and applications with MVVM pattern? I would like to use in day-to-day activities to make my life easier.

Please do provide your thoughts.
0
Comment
Question by:Easwaran Paramasivam
7 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 38745809
fairly effectively.
0
 
LVL 23

Assisted Solution

by:Michael74
Michael74 earned 200 total points
ID: 38746001
I have always liked log files.

If you create a static class that is called to log the system status you can capture the state of application to a number of different levels at any point in the code. I have attached a static class I created for a project I completed that will just that. Note: If you override the ToString method for your model classes you will get a more readable result.

Below is an example I develop for one of my projects. One advantage of this method is the fact that logging can controlled without code changes

Michael

In app.config
    <!-- Trace Level 0=none, 1=errors, 2=warnings, 3=info, 4=verbose -->
    <add key="trace" value="0" />

Open in new window


Static class
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;

namespace Status.Presenter.Static_Classes {
   static class Logger {
      /************************************************************************************************************************************/
      #region Public Variables

      public static TraceListener listener;
      
      #endregion
      /************************************************************************************************************************************/
      #region Private Constants

      private static string LOGFILE = "Status.log";
      private static string OLDLOG = "Status.log.old";
      private static int MAXLOGMB = 10;

      #endregion
      /************************************************************************************************************************************/
      #region Public Methods

      // Create and Initialise Trace Listener
      public static void InitialiseListener() {
         if ((Convert.ToInt32(ConfigurationManager.AppSettings["trace"]) > 0)) {
            listener = new TextWriterTraceListener(LOGFILE, "myListener");
            Trace.Listeners.Add(listener);
         }
      }

      // Remove and Close Trace Listener. No Action on error.
      public static void CloseListener() {
         try {
            listener.Close();
            Trace.Listeners.Remove(listener);
         }
         catch (Exception) { }
      }

      /* Write Error details to log file if trace level higher than 0 and write trace details if trace level higher than 3 */
      public static void Error(Exception ex, string module, string errorMessages) {
         if (Convert.ToInt32(ConfigurationManager.AppSettings["trace"]) >= 1) {
            WriteError(ex, "Error", module, errorMessages);
            if (Convert.ToInt32(ConfigurationManager.AppSettings["trace"]) >= 3) {
               Trace.WriteLine("Trace\r\n" + ex.StackTrace + "\r\n");
            }
            Trace.Flush();
         }
      }

      // Write Error details to log file if trace level higher than 1
      public static void Error(string module, string message) {
         if (Convert.ToInt32(ConfigurationManager.AppSettings["trace"]) >= 1) { WriteMessage(module, message); }
      }

      // Write Error details to log file if trace level higher than 1
      public static void Error(object obj) {
         if (Convert.ToInt32(ConfigurationManager.AppSettings["trace"]) >= 1) {
            Trace.WriteLine(obj);
            Trace.Flush();
         }
      }

      // Write Error details to log file if trace level higher than 1
      public static void Error(string message) {
         if (Convert.ToInt32(ConfigurationManager.AppSettings["trace"]) >= 1) {
            Trace.WriteLine(message);
            Trace.Flush();
         }
      }

      // Write Warning details to log file if trace level higher than 1
      public static void Warning(Exception ex, string module, string errorMessages) {
         if (Convert.ToInt32(ConfigurationManager.AppSettings["trace"]) >= 2) {
            WriteError(ex, "Warning", module, errorMessages);
            Trace.Flush();
         }
      }

      // Write Warning details to log file if trace level higher than 1
      public static void Warning(string module, string message) {
         if (Convert.ToInt32(ConfigurationManager.AppSettings["trace"]) >= 2) { WriteMessage(module, message); }
      }

      // Write LogInfo details to log file if trace level higher than 2
      public static void Info(string module, string message) {
         if (Convert.ToInt32(ConfigurationManager.AppSettings["trace"]) >= 3) { WriteMessage(module, message); }
      }

      // Write Verbose details to log file if trace level higher than 3
      public static void Verbose(string module, string message) {
         if (Convert.ToInt32(ConfigurationManager.AppSettings["trace"]) >= 4) { WriteMessage(module, message); }
      }

      // Write Verbose details to log filen using to ToString method of the object if trace level higher than 3
      public static void Verbose(object obj) {
         if (Convert.ToInt32(ConfigurationManager.AppSettings["trace"]) >= 4) {
            Trace.WriteLine(obj);
            Trace.Flush();
         }
      }

      // Write Verbose details to log file if trace level higher than 3
      public static void Verbose(string message) {
         if (Convert.ToInt32(ConfigurationManager.AppSettings["trace"]) >= 4) {
            Trace.WriteLine(message);
            Trace.Flush();
         }
      }

      // Write Stack Trace
      public static void Stack() {
         if (Convert.ToInt32(ConfigurationManager.AppSettings["trace"]) > 0) {
            Trace.WriteLine(Environment.StackTrace + "\r\n");
            Trace.Flush();
         }
      }

      #endregion
      /************************************************************************************************************************************/
      #region Private Methods

      // Write an error to the log file
      private static void WriteError(Exception ex, string type, string module, string errorMessages) {
         if (ErrorFileRollRequired()) { RollErrorFile(); }
         Trace.WriteLine(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss")+ "\t" + type + 
                         "\t\t\t" + module + "\r\n" +
                         "\t\t\t" + "Ellipse Error Messages - " + errorMessages + "\r\n" +
                         "\t\t\t" + "Exception Message - " + ex.Message + "\r\n");
      }

      // Write a message to the log file
      private static void WriteMessage(string module, string message) {
         if (ErrorFileRollRequired()) { RollErrorFile(); }
         Trace.WriteLine(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + "\t" + module + " - " + message);
         Trace.Flush();
      }

      // Test if Error File exceeds Maximum size as defined by MAXLOGMB
      private static bool ErrorFileRollRequired() {
         if (!File.Exists(LOGFILE)) { return false; }
         if (new FileInfo(LOGFILE).Length / 1024 / 1024 < MAXLOGMB) { return false; }
         return true;
      }

      // Rename current log as Bus Status.log.old and open new log file
      private static void RollErrorFile() {
         CloseListener();
         try {
            if (File.Exists(OLDLOG)) { File.Delete(OLDLOG); }
            File.Move(LOGFILE, OLDLOG);
            File.Delete(LOGFILE);
         }
         catch { }
         finally { InitialiseListener(); }
      }

      #endregion
      /************************************************************************************************************************************/
   }
}

Open in new window

0
 
LVL 19

Assisted Solution

by:Manoj Patil
Manoj Patil earned 100 total points
ID: 38749092
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 16

Author Comment

by:Easwaran Paramasivam
ID: 38750059
I'm looking for most handful debugging ways such as what are the free tools or VS.NET 2008 tools  could be used to debug the application easily?
0
 
LVL 23

Expert Comment

by:Michael74
ID: 38750213
Logging is one method
Placing break points and stepping through the code is another
Unit testing, that is testing just the model or view in isolation is one of benefits of the MVVM design pattern.

Michael
0
 
LVL 16

Author Comment

by:Easwaran Paramasivam
ID: 38750236
Could you please explain in detail?
0
 
LVL 23

Accepted Solution

by:
Michael74 earned 200 total points
ID: 38758299
1/ Unit testing means writing quick test classes that enable you to test modules in isolation. So to test your model you import the classes into a new project and call the methods with dummy data to test the output. This allows you test only the model only.

Some more info can be found here
http://wiki.developerforce.com/page/How_to_Write_Good_Unit_Tests

2/ Break points
http://geekswithblogs.net/sdorman/archive/2009/02/14/visual-studio-2008-debugging-tricks-ndash-advanced-breakpoints.aspx

3/ You logging you can use the static class I provided above and then place calls to log the state of the system before, during and after methods calls as required. This allows you see the data of a process without having to step through the entire code.

Michael
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now