Solved

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

Posted on 2013-01-04
7
471 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

763 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