Solved

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

Posted on 2013-01-04
7
467 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need to pass connection string through config 2 26
Vb.net dynamic formulas in runtime 11 61
Error in script 11 46
Error in JQuery 5 38
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

22 Experts available now in Live!

Get 1:1 Help Now