?
Solved

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

Posted on 2013-01-04
7
Medium Priority
?
476 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:Michael Fowler
Michael Fowler earned 800 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 400 total points
ID: 38749092
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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:Michael Fowler
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:
Michael Fowler earned 800 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

762 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