How do I do implement a trace?



How do I do implement a trace?..

Answer / 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

More C Sharp Interview Questions

What is string in c# net?

1 Answers  


Why should you override the tostring() method?

1 Answers  


What is a event in c#?

1 Answers  


Is lazy thread safe c#?

1 Answers  


What is response redirect in c#?

1 Answers  


Which is algorithm is used to Manage the Memory in .net ??

7 Answers   TCS,


What are the advantages of interface in c#?

1 Answers  


What is string method in c#?

1 Answers  


What is lambda expression in c#?

1 Answers  


What are the advantages of using delegates in c#?

1 Answers  


What is the data encapsulation?

1 Answers  


What is the CTS, and how does it relate to the CLS?

1 Answers   Arigo Infotech,


Categories