Solved

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

Posted on 2013-01-04
7
473 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Securing WEBAPI on Azure 2 55
asp.net repeater 2 35
Removing Version of .NET in Windows 10 17 121
How does this modal work? 3 33
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

732 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