By default, ASP. Net does not have the permission to write system logs. To write system logs in ASP. Net, you must have the permissions first. We can solve this problem by setting or modifying the Registry Permissions.
Method 1: "START-> Run", enter the command, "regedt32", and find "System-> CurrentControlSet-> Services-> Eventlog ", select "security"> "Permissions"> "add", find the "AspNet" user on the local machine, add the user, and grant the read permission, an "aspnet_wp account" is added to the directory"
Method 2: add the full User permission to the Registry: HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Service \ EventLog.
Using System;
Using System. Diagnostics;
Using System. Text;
Namespace CorePlus. Framework. Utility
{
/// <Summary>
/// The log-writing CLASS
/// </Summary>
Public class LogUtility
{
Public enum EVENT
{
/// <Summary> major error </summary>
EVENT_ERROR,
/// <Summary> operation failed </summary>
EVENT_FAILUREAUDIT,
/// <Summary> General Information </summary>
EVENT_INFORMATION,
/// <Summary> valid and successful operations </summary>
EVENT_SUCCESSAUDIT,
/// <Summary> warning </summary>
EVENT_WARNING,
}
Private static TextWriterTraceListener listner = null;
/// <Summary>
/// Constructor
/// </Summary>
Public LogUtility (){}
/// <Param name = "loginID"> logon ID </param>
/// <Param name = "screenID"> image ID </param>
/// <Param name = "level"> warning level </param>
/// <Param name = "message"> log information </param>
Public static void Logging (
String loginID,
String screenID,
EVENT level,
String message)
{
// Variable definition
StringBuilder outputMessage = null; // output information
String targetLevelString = null;
EventLogEntryType Eventtype = new EventLogEntryType ();
Switch (level)
{
Case EVENT. EVENT_ERROR:
Eventtype = EventLogEntryType. Error;
Break;
Case EVENT. EVENT_FAILUREAUDIT:
Eventtype = EventLogEntryType. FailureAudit;
Break;
Case EVENT. EVENT_INFORMATION:
Eventtype = EventLogEntryType. Information;
Break;
Case EVENT. EVENT_SUCCESSAUDIT:
Eventtype = EventLogEntryType. SuccessAudit;
Break;
Case EVENT. EVENT_WARNING:
Eventtype = EventLogEntryType. Warning;
Break;
}
// Combine log information
OutputMessage = MakeMessage (loginID, screenID, targetLevelString, message );
// Write logs
Logging (Eventtype, outputMessage. ToString ());
}
/// <Summary>
/// Write logs to the log Manager
/// </Summary>
/// <Param name = "level"> error level </param>
/// <Param name = "message"> output information </param>
Private static void Logging (EventLogEntryType level, string message)
{
// Variable definition
String logName = null; // log name
String machineName = null; // machine name
String sourceName = null; // SourceName
EventLog eventLog = null; // EventLog
LogName = "Eventlog2"; // log name
MachineName = "."; // machine name
SourceName = "Eventlog2"; // SourceName
EventLog = new EventLog (logName, machineName, sourceName );
EventLog. WriteEntry (message, level );
EventLog = null;
}
/// <Summary>
/// Finally combine logs
/// </Summary>
/// <Param name = "loginID"> logon ID </param>
/// <Param name = "screenID"> image ID </param>
/// <Param name = "levelString"> error level </param>
/// <Param name = "message"> log information </param>
/// <Returns> output log information </returns>
Private static StringBuilder MakeMessage (
String loginID,
String screenID,
String levelString,
String message)
{
// Variable definition
StringBuilder retMessage = new StringBuilder ();
RetMessage. Append ("[Login ID ]");
RetMessage. Append (loginID );
RetMessage. Append ("\ n [Image ID ]");
RetMessage. Append (screenID );
RetMessage. Append ("\ n [error level ]");
RetMessage. Append (levelString );
RetMessage. Append ("\ n [error message] \ n ");
RetMessage. Append (message );
Return retMessage;
}
}
}
// Output logs in FORM
Private void button#click (object sender, System. EventArgs e)
{
LogUtility. Logging ("bbbbbb", "22222222", LogUtility. EVENT. EVENT_ERROR, "3333333 ");
}
After running the program, open the log manager and see ~~~~~~~~~ How is it? Good, huh, huh.
Another method
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Diagnostics;
Namespace Log
{
Class LogWirter
{
/// <Summary>
/// Event source name
/// </Summary>
Private string eventSourceName;
EventLogEntryType eventLogType;
Public LogWirter ()
{
EventSourceName = "test ";
EventLogType = EventLogEntryType. Error;
}
/// <Summary>
/// Message event source name
/// </Summary>
Public string EventSourceName
{
Set {eventSourceName = value ;}
}
/// <Summary>
/// Message Event Type
/// </Summary>
Public EventLogEntryType EventLogType
{
Set {eventLogType = value ;}
}
/// <Summary>
/// Write System Logs
/// </Summary>
/// <Param name = "message"> event content </param>
Public void LogEvent (string message)
{
If (! EventLog. SourceExists (eventSourceName ))
{
EventLog. CreateEventSource (eventSourceName, "Application ");
}
EventLog. WriteEntry (eventSourceName, message, EventLogEntryType. Error );
}
}
}