user316117
user316117

Reputation: 8271

Visual Studio debug log (like logcat in Android)?

I'm trying to debug a C#/.Net program in Visual Studio 2010 but it's timing-sensitive so breakpoints mess it up. So I just want to log certain events and their relative times and watch them as my program is running.

Does Visual Studio (or C# or .Net) have any facility for writing to a log file that Visual Studio can display as a debug window?

When I do Android development in Eclipse I can take advantage of their Log class and insert lines that looks like this in my code:

  Log.d("Label1", "Hit checkpoint X");  

And they will appear along with time/date stamps in the LogCat window which is displayed along with other windows in Eclipse.

What's the closest equivalent to this in Visual Studio 2010?

Upvotes: 7

Views: 8283

Answers (4)

Sorry IwontTell
Sorry IwontTell

Reputation: 502

Use Android Logcat Output plugin:
https://marketplace.visualstudio.com/items?itemName=LancelotChen.AndroidLogcatOutput

Visual Studio Tool Window Extension for Android Logcat Output & Filter

Inspired by NVidia Nsight Tegra.

To launch the logcat output window, use Menu

View --> Other Windows --> Logcat Output

Then the logcat output window will show.

On the left pane is filter list, you can add new filetr. Make one filter checked, the logs will be filtered.



Right-click on filter will bring out conext menu, which you can use to delete or edit filter settings.

Upvotes: 1

NickGlowsinDark
NickGlowsinDark

Reputation: 337

For whatever reason, this question was at the top of my Google search when looking for something similar (though it's 5 years old), so in case anyone else comes across it, this is the easiest method I've encountered:

Debug.WriteLine("hit checkpoint X");

It's included in System.Diagnostics, so you'll need a using System.Diagnostics at the top of your file, but that's all you need.

Upvotes: 2

JNYRanger
JNYRanger

Reputation: 7097

There isn't really anything built in exactly like logcat, but there are lots of logging frameworks that you can use.

Personally, I like to use NLog and set up a UDP target for tracing/debugging within my configuration file along with a rule to forward all loggers to the target. I think that NLog is easier to use than Log4Net (the .NET port of Log4j). Once you do this you can create a logger from the manager and call the logger just like LogCat in android:

Logger logger = LogManager.GetLogger("MyClassTag");

logger.Trace("Something to log");
logger.Debug("Something to log");
logger.Info("Something to log");
logger.Warn("Something to log");
logger.Error("Something bad to log", exception);
logger.Fatal("Something bad to log", exception);

For listening to the UDP logging packets I use Log2Console which allows me to view them just like android's logcat viewer.

Upvotes: 2

heavyd
heavyd

Reputation: 17731

You can set a breakpoint that doesn't actually break, but outputs a log message instead. Just set a breakpoint, then right click on the breakpoint and select "When Hit...".

From that dialog select "Print a message" and "continue execution"

Dialog screenshot

Upvotes: 6

Related Questions