Posts Tagged: Editors


4
Nov 08

Tracing in C#

Recently I went thru’ a article that talks about Conditional Debugging in C#. Eager to try it out, I was researching it. I was really surprised to see the out comes. So I thought of sharing all those things with you. As the above has link already talked about conditional debugging, lets start with tracing.

In C#, there are loads of ways to trace the execution. All of them can be acheived using the object Trace. You can trace the execution and the errors that are thrown by the application in to a,

Console

Here what ever you trace will be written to the Console (command prompt), if you run the application from a command prompt. To accomplish this we need to do the following,

  • Create an object of the ConsoleTraceListener ConsoleTraceListener aConsoleTraceListener = new ConsoleTraceListener();  //Creating an object for tracing
  • In the place where you wanna trace something, add the following statementaConsoleTraceListener.WriteLine("<the message you wish to trace on executing the current method>");  //Will write a trace line in to the console

This will work only if you are running a console application or a window application from the console (You can also change the properties of a window application to console application and then run it so that it automatically runs from the console even if you press F5 from Visual Studio).

EventViewer

There is a utility called EventViewer that comes with windows. Try typing eventvwr in Run applications. You will get a dialog that has loads of events and their logs. You can create an object of EventLogTraceListener to log the events out there. Lets see how we do it.

  • Create an object of the EventLogTraceListenerEventLogTraceListener aEventLogTraceListener = new EventLogTraceListener("Foo");  //Creating an object of the trace viewer with the source name as Foo
  • In the place where you wanna trace something, add the following statementaEventLogTraceListener.WriteLine("<the message you wish to trace on executing the current method>");  //Will write a trace line in to the event viewer

 

Additional options

The above methods allows you to trace some messages. In addition if you need to trace the ThreadID, ProcessID, TimeStamp or even CallStack, you can use the TraceEventCache. This class allows one to get the above mentioned properties of a event. Try adding the following snippet to your code.

TraceEventCache aTraceEventCache = new TraceEventCahce();  //New trace cache object that will hold additional info about the event

aConsoleTraceListener.TraceOutputOptions = TraceOptions.TimeStamp | TraceOptions.ProcessId;  //Will add these details to the log

The TraceEvent method will log additional details like TimeStamp, ProcessID, etc., to the message that is being logged. These are some of the methods to trace the flow or execution of a program. These are some of the easy ways to find the bugs(if any) in our application even if it has been released to the customers irrespective of the nature of the build (debug or release).

 

aConsoleTraceListener.TraceEvent(aTraceEventCache, “Foo”, TraceEventtype.Information, 0, “Method __name___ has been executed”);  //Will log the trace message in to the console window

 

Update

To log to a text stream you can use the TextWriterTraceListener object. You can also use the static class EventLog to trace messages to the EventViewer.


3
Nov 08

Building C# applications from command prompt

Ever wondered how things will be if there is no Visual Studio ?? Even the compilation of a C# application (especially windows based application) is kinda difficult. Dare to dream soo ? Lets try to build a window based application without visual studio’s source builder.

  • Open Visual Studio and create a new Windows Application, say Foo. Its target will be an executable (exe)
  • Grab some buttons in to the form (simplly. No specific use)
  • Now open your visual studio command prompt. This is not something really Alien. This is the ordinary command prompt with some pre applied commands, rather some paths and variables set
  • Go to the folder where you have created your project. Consider that I have Foo under my E:\tries. then, navigate to cd E:\tries\Foo\Foo always prefer creating solution in a seperate directory. Thats why Foo/Foo
  • Now try typing csc Program.cs and check what happenscsc Program.cs
  • You will get some error stating that it cannot find Form1. Allrite. Lets solve it. This occurs because you are trying to build Program.cs alone without Form1 (Open and check Program.cs file. You will see an object of Form1 inside the Main() method). Leave it.
  • Lets try building Form1.cs

    csc Form1.cs     

  • Again a build error pops up. This is due to the partail classes. Gosh. You can solve all these error by using the recursive build. i.e. Progam.cs actually needs Form1 which in turn needs something else. Try typing

    csc /recurse:*.cs

  • The above command will build all the .cs files in the way it needs. Now relax.
  • Sometimes you may get other errors, like assembly not referenced. How to resolve those ?? How to add assemblies in command prompt ??? There are loads of options for the csc. I have discussed some of them below

Recursively build files

To automaically build files in a specific order and to do it recursively, we can use

csc /recurse:*.cs

Attaching references

To attach a reference we can use the /reference: option (/r:) in short

csc /r: System.Windows.Forms.dll /recurse:*.cs

Specifying the output (target) for a project

We can even specify the type of a target and the name if it using the /out option

csc /r: System.Windows.Forms.dll /recurse: *.cs /out:Foo.exe

Specifying debug and release builds

If you wanna run a debug build, use the /define option

csc /r: System.Windows.Forms.dll /recurse:*.cs /out:Foo.exe /define:DEBUG

Change it to whatever your conditional parameter is. Running the above will enable all the traces and will be seen in the console itself.

There are loads more in the MSDN library. Check the compiler options for more.


6
Sep 08

Visual studio shortcuts

Its always time consuming to use all the features provided by an editor while coding (if you don;t know the hotkeys). For example, Visual Studio. When you are using a big editor like Visual Studio, you will have soo many functionalities to use while you code. Most of the time, these features goes unnoticed when you can’t find the shortcuts for those. Also the shortcuts will be too big to remember. Below mentioned ones are really handy. I will be talking from the C# point of view. So I really don’t know whether all these will work for all the languages. Continue reading →