Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swdev:dotnet:debugging [2011/03/25 09:39]
smayr
swdev:dotnet:debugging [2011/08/30 16:34] (current)
smayr [Debugging]
Line 1: Line 1:
 == Debugging == == Debugging ==
 +
 +  * Enable WPF debug output. In Visual Studio, Options > Debugging > Output Window > WPF Trace Settings > Data Binding > All.
 +  * Add a high TraceLevel to your binding: <code csharp>System.Diagnostics.PresentationTraceSources.SetTraceLevel(NewBinding, System.Diagnostics.PresentationTraceLevel.High);</code>
 +  * Run application, and review log in "Output" tab in Visual Studio.
 +  * Add ''System.Diagnostics'' entry to ''app.exe.config'' See:
 +    * [[swdev:dotnet:debugging|Debugging for .NET]]
 +    * [[http://www.thejoyofcode.com/System.Diagnostics_hidden_SourceLevels.aspx|The Joy of Code: System.Diagnostics Hidden SourceLevels]] 
 +    * [[http://msdn.microsoft.com/en-us/library/aa345169.aspx|MSDN: PresentationTraceSources Class]]
 +    * [[http://msdn.microsoft.com/en-us/library/system.diagnostics.presentationtracesources.settracelevel.aspx|MSDN: SetTraceLevels]]
 +    * [[http://msdn.microsoft.com/en-us/library/6w20x90k.aspx|MSDN: EventLog.WriteEntry method]]
 +  * [[http://stackoverflow.com/questions/4026543/is-there-a-good-tool-for-debugging-xamls-databinding-behavior-errors-at-runtim|Tools for Debugging Data Binding at runtime]]
 +  * [[http://bea.stollnitz.com/blog/?p=52|Bea Stollnitz: How can I debug WPF bindings?]]
  
 == Using System.Diagnostics == == Using System.Diagnostics ==
  
 +=== Configuration File ===
 Add the following to the ''//app//.exe.config'' file (must reside in the same folder and the ''//app//.exe''): Add the following to the ''//app//.exe.config'' file (must reside in the same folder and the ''//app//.exe''):
 <code xml> <code xml>
Line 11: Line 24:
       <source name="srcMyAppDebug" switchName="SourceSwitch" >       <source name="srcMyAppDebug" switchName="SourceSwitch" >
         <listeners>         <listeners>
-          <add name="textListener" />+          <add name="myTextListener" />
         </listeners>         </listeners>
       </source>       </source>
Line 27: Line 40:
  
 </configuration> </configuration>
 +</code>
 +
 +The ''SourceLevels'' used by the ''SourceSwitch'' (''//app//.exe.config'' configuration file) could be any of these:
 +<code csharp>
 +public enum SourceLevels
 +{
 +    All             = -1,
 +    Off             = 0,
 +    Critical        = 1,
 +    Error           = 3,
 +    Warning         = 7,
 +    Information     = 15,
 +    Verbose         = 31,
 +    ActivityTracing = 65280,
 +}
 </code> </code>
  
Line 34: Line 62:
      <add name="myXmlListener"      <add name="myXmlListener"
           type="System.Diagnostics.XmlWriterTraceListener"           type="System.Diagnostics.XmlWriterTraceListener"
-          initializeData= "Debug.txt" />+          initializeData= "Debug.xml" />
   </listeners>   </listeners>
 </code> </code>
 +
 +=== Writing Events ===
  
 To write to the event log: To write to the event log:
Line 46: Line 76:
 Alternatively, and more flexible: Alternatively, and more flexible:
 <code csharp> <code csharp>
-TraceSource srcMyApp = new TraceSource("srcMyAppp");+TraceSource srcMyApp = new TraceSource("srcMyAppDebug");
 srcMyApp.TraceInformation("Some operation here: Hello World"); srcMyApp.TraceInformation("Some operation here: Hello World");
 srcMyApp.TraceEvent(TraceEventType.Error, 0, "Error: Something failed here."); srcMyApp.TraceEvent(TraceEventType.Error, 0, "Error: Something failed here.");
 +</code>
 +
 +The ''TraceEventType'' could be any of these:
 +<code csharp>
 +public enum TraceEventType
 +{
 +    Critical    = 1,
 +    Error       = 2,
 +    Warning     = 4,
 +    Information = 8,
 +    Verbose     = 16,
 +    Start       = 256,
 +    Stop        = 512,
 +    Suspend     = 1024,
 +    Resume      = 2048,
 +    Transfer    = 4096,
 +}
 +</code>
 +
 +Another example:
 +<code csharp>
 +using System.Diagnostics;
 +
 +private void WriteToEventLog(string message)
 +{
 +  string cs = "srcMyApp";
 +  EventLog elog = new EventLog();
 +
 +  if (!EventLog.SourceExists(cs))
 +  {
 +     EventLog.CreateEventSource(cs, cs);
 +  }
 +
 +  elog.Source = cs;
 +  elog.EnableRaisingEvents = true;
 +  elog.WriteEntry(message);
 +}
 +</code>
 +
 +Another example (by Microsoft):
 +<code csharp>
 +using System;
 +using System.Diagnostics;
 +using System.Threading;
 +
 +class MySample{
 +
 +    public static void Main(){
 +
 +        // Create the source, if it does not already exist.
 +        if(!EventLog.SourceExists("MySource"))
 +        {
 +             //An event log source should not be created and immediately used.
 +             //There is a latency time to enable the source, it should be created
 +             //prior to executing the application that uses the source.
 +             //Execute this sample a second time to use the new source.
 +            EventLog.CreateEventSource("MySource", "MyNewLog");
 +            Console.WriteLine("CreatedEventSource");
 +            Console.WriteLine("Exiting, execute the application a second time to use the source.");
 +            // The source is created.  Exit the application to allow it to be registered.
 +            return;
 +        }
 +
 +        // Create an EventLog instance and assign its source.
 +        EventLog myLog = new EventLog();
 +        myLog.Source = "MySource";
 +
 +        // Write an informational entry to the event log.    
 +        myLog.WriteEntry("Writing to event log.");
 +
 +    }
 +}
 </code> </code>