C # Read windows logs

Source: Internet
Author: User

Logs can be recorded in the global database.
// Record the exceptions not handled in the ASP. NET site
Protected void application_error (Object sender, eventargs E)
{
Exception EX = server. getlasterror (). getbaseexception ();
Logentry log = new logentry ();
Log. categories. Add ("general ");
Log. Title = "unprocessed exceptions ";
Log. eventid = 1;
Log. Message = ex. Message +
"\ R \ nsource:" + ex. Source +
"\ R \ nform:" + request. Form. tostring () +
"\ R \ nquerystring:" + request. querystring. tostring () +
"\ R \ ntargetsite:" + ex. targetsite +
"\ R \ nstacktrace:" + ex. stacktrace;
Logger. Write (log );
}

Test environment:. NET Framework 2.0, Windows Server 2003 sp2, Visual Studio 2005 SP1

Using System;
Using System. Data;
Using System. configuration;
Using System. collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. Web. UI. htmlcontrols;
Using System. text;

UsingSystem. diagnostics;

Public Partial Class Default3: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{
// Windows logs include: "application" applicationsProgram, "Security", "system" System
String [] Logs = New String [] { " Application " , " System " };

Stringbuilder result= NewStringbuilder ();

Foreach ( String Log In Logs)
{
EventLog mylog = New EventLog ();
Mylog. Log = Log;
// Mylog. machinename = "rondi-agt0qf9op ";
Foreach (Eventlogentry entry In Mylog. Entries)
{
// Eventlogentrytype enumeration includes:
// Error Event.
// Failureaudit failure Audit Event.
// Information events.
// Successaudit successfully reviews the event.
// Warning event.
If (Entry. entrytype = Eventlogentrytype. Error | Entry. entrytype = Eventlogentrytype. Warning)
{
Result. append ( " <Font color = 'red'> " + Log );
Result. append (entry. entrytype. tostring () + " </Font> " );
Result. append ( " <Font color = 'blue'> ( " + Entry. timewritten. tostring () + " ) </Font>: " );
Result. append (entry. Message + " <Br/> " );
}
}
}
Response. Write (result. tostring ());
}
}

Output result:

Applicationwarning (16:25:49): Event code: 3005 event message: An unhandled exception occurred. Event Time: 2007-6-16 16:25:49 Event Time (UTC)

Systemwarning (10:34:15): The Browser Service cannot retrieve the server list from the main browser \ qianting01 on the network \ device \ netbt_tcpip _ {5ac78e63-d3b3-1086-8ff7-d8d2b41b3620. Main Browser: \ qianting01 Network: \ device \ netbt_tcpip _ {5ac78e63-d3b3-4266-8ff7-d8d2b41b3620} this event may be caused by temporary loss of network connections. If this message appears again, make sure the server is still connected to the network. The return code is in the data text box.

Systemerror (10:34:45): The Browser Service has been unable to capture the backup list on \ device \ netbt_tcpip _ {5ac78e63-d3b3-1166-8ff7-d8d2b41b3620} transmission many times. The backup browser has stopped.

Systemerror (15:40:30): The Browser Service has been unable to capture the backup list on \ device \ netbt_tcpip _ {5ac78e63-d3b3-1166-8ff7-d8d2b41b3620} transmission many times. The backup browser has stopped.

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.