using System; using System.Diagnostics; // ... try { // Some application code here } catch(Exception ex) { // Log the exception to the Event Viewer EventLog.WriteEntry("ApplicationName", ex.Message, EventLogEntryType.Error); }
using System; using System.Diagnostics; // ... // Create a new TraceSource var traceSource = new TraceSource("ApplicationName", SourceLevels.All); // Log a custom event traceSource.TraceEvent(TraceEventType.Information, 0, "Custom message");
using System.Diagnostics; // ... // Read the configuration file var configFile = new System.Configuration.ExeConfigurationFileMap { ExeConfigFilename = "path/to/config/file.config" }; var config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None); // Get the TraceSource settings var traceSourceSettings = config.GetSection("system.diagnostics") as System.Diagnostics.TraceSource; // Set the TraceSource verbosity var traceSource = new TraceSource(traceSourceSettings.Name, SourceLevels.All);In conclusion, tracing framework is a powerful tool for monitoring and debugging applications in real-time. The System.Diagnostics package in C# provides developers with an easy-to-use and highly configurable way to capture and log events and errors produced during the execution of their applications.