Record error messages to Windows logs

Source: Internet
Author: User
Tags error handling try catch
Introduction:
It is impossible for any system to run without errors once and for all. The error handling of an excellent system must be excellent, a good programmer will also pay close attention to possible errors and handle fault tolerance accordingly. Try catch in C # saves a lot of trouble for us. This article does not discuss how to handle errors, but provides a collection of error information, and store the error information in Windows logs.
I. Processing code:
Using System;
Using System. Diagnostics;
Using System. Text;
Using System. IO;

Namespace Town. Log
{
/// <Summary>
/// Function: error log class, which records error information in system logs according to the specified event log name
/// </Summary>
Public class Error
{
/// <Summary>
/// Record logs
/// </Summary>
/// <Param name = "sourceName"> Log resource name, for example, Town </param>
/// <Param name = "message"> error message </param>
Public static void Log (string sourceName, string message)
{
EventLog eventLog = null;

// Determine whether the log exists
If (! (EventLog. SourceExists (sourceName )))
{
EventLog. CreateEventSource (sourceName, sourceName + "Log ");
}

If (eventLog = null)
{
EventLog = new EventLog (sourceName + "Log ");
EventLog. Source = sourceName;
}

// Record log information
EventLog. WriteEntry (message, System. Diagnostics. EventLogEntryType. Error );
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.