Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / C#

How to log the current call stack in .NET

Rate me:
Please Sign up or sign in to vote.
4.69/5 (11 votes)
10 Jul 2011CPOL1 min read 100K   1.6K   45  
This article explains how to log the current call stack by using the StackFrame and StackTrace classes of the .NET Framework.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Mazzeranghi.StackLogger
{
  public partial class MainFrame : Form
  {
    public MainFrame()
    {
      InitializeComponent();
    }

    private void ShowLog()
    {
      LogDisplay.Text = Logger.GetCurrentLog();
    }

    private void InvalidOperation_Click(Object p_Sender, EventArgs p_EventArgs)
    {
      try
      {
        var l_Account = new BankAccount(100);
        l_Account.Withdraw(150);
      }
      catch (Exception p_Exception)
      {
        Logger.LogMessage(p_Exception.ToString());
      }
      ShowLog();
    }

    private void ValidOperationWithDefaultLog_Click(Object p_Sender, EventArgs p_EventArgs)
    {
      var l_Account = new BankAccount(100);
      l_Account.Withdraw(50);
      ShowLog();
    }

    private void ValidOperationWithCustomizedLog_Click(Object p_Sender, EventArgs p_EventArgs)
    {
      var l_Account = new BankAccount(100);
      l_Account.Deposit(50);
      ShowLog();
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Esaote
Italy Italy

Since 1998, I'm a software project leader at Esaote, one of the world's major manufacturers of non-invasive diagnostic equipment and system, as well as the leading European manufacturer of ultrasound imaging equipment.


My professional skills include:



  • Programming Languages: C#, C/C++, ASP.NET, XML, XSLT, Prolog.
  • Libraries: .NET Framework, STL, MFC.
  • Paradigms and Standards: Object-Oriented Programming, Design Patterns, Code Generation, Extreme Programming, Design by Contract, Dicom.
  • Tools: Visual Studio .NET, SQL Server.

You can contact me at my Web site (www.mazzeranghi.com).


Comments and Discussions