adspace


How do I do implement a trace?

Answer Posted / Abhinav Kumar

To implement a trace in C#, you can use the System.Diagnostics namespace. You need to create an instance of EventLogTraceListener and attach it to TextWriterTraceListener. Then set the source name and write your messages using Trace methods such as Trace.WriteLine(string). Here is an example:nn```csharpnusing System;
using System.Diagnostics;
class Program
{n static void Main(string[] args) {n using (var traceSource = new EventLogTraceSource("MyTraceSource", "My Application")) {n traceSource.Switch = SourceLevels.Information;n var traceListener = new TextWriterTraceListener("trace.txt");
var eventLogTraceListener = new EventLogTraceListener("Application");
Trace.Listeners.Add(traceListener);
Trace.Listeners.Add(eventLogTraceListener);
Trace.WriteLine("Hello, World!");
}
}n```

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why can't we use a static class instead of singleton?

954


How do you inherit a class into other class in c#?

993


Which namespaces are necessary to create a localized application?

1142


What is an abstract class c#?

969


What is expression tree in c#?

998


How to assign Null value to Var?

1063