- Open Visual Studio C#.
- Create a new Console application in Visual C#. The Console application creates a public class and an empty Main method for you.
- Verify that the project references at least the System.dll file.
- Use the using directive on the System and System.Diagnostics namespaces so that you do not have to qualify declarations from these namespaces later in your code. You must use these statements before any other declarations.
using System;
using System.Diagnostics; - To write to an event log, you must have several pieces of information: Your message, the name of the log you to which you want to write (which will be created if it does not already exist), and a string that represents the source of the event. You can register a particular source with only a single event log; if you want to write messages to more than one log, you must define multiple sources.
string sSource;
string sLog;
string sEvent;
sSource = "dotNET Sample App";
sLog = "Application";
sEvent = "Sample Event"; - Use two static methods of the EventLog class to check whether your source exists, and then, if the source does not exist, to create this source that is associated with a particular event log. If the log name that you specify does not exist, the name is created automatically when you write your first entry to the log. By default, if you do not supply a log name to the CreateEventSource method, the log file is named "Application Log."
if (!EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource,sLog); - To write a message to an event log, you can use the EventLog.WriteEntry static method. This method has several different overloaded versions. The following sample code shows the simplest method, which takes a source string and your message, and one of the more complex methods, which supports specifying the event ID and event type:
EventLog myLog = new EventLog();
myLog.Log = sLog;
myLog.Source = sSource;
myLog.WriteEntry(sEvent); - Save your application. Run your application, and then check the Application log in the Event Viewer to see your new events.
using System;
using System.Diagnostics;
namespace WriteToAnEventLog_csharp
{
/// Summary description for Class1.
class Class1
{
static void Main(string[] args)
{
string sSource;
string sLog;
string sEvent;
sSource = "dotNET Sample App";
sLog = "Application";
sEvent = "Sample Event";
if (!EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource,sLog);
EventLog myLog = new EventLog();
myLog.Log = sLog;
myLog.Source = sSource;
myLog.WriteEntry(sEvent);
}
}
}
If you log on to a computer as a regular user, and if you try to use Microsoft Visual Studio .NET to create a custom event log to register events, you may receive the following error message:
An unhandled exception of type 'System.Security.SecurityException' occurred in mscorlib.dll
Additional information: Requested registry access is not allowed.
Grant permission to create a custom event log
Additional information: Requested registry access is not allowed.
Grant permission to create a custom event log
- Log on to the computer as an administrator.
- Click Start, click Run, type regedit in the Open box, and then click OK. The Registry Editor window appears.
- Locate the following registry subkey:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog
- Right-click Eventlog, and then click Permissions. The Permissions for Eventlog dialog box appears.
- Click Advanced. The Advanced Security Settings for Eventlog dialog box appears.
- In the Name column, double-click the Users group. The Permission Entry for Eventlog dialog box appears.
- Select the Set Value check box, select the Create Subkey check box, and then click OK.
(if this doesn't work, select full control check box and click OK.) - Quit Registry Editor, and then log off from the administrator account.
- Log on to the computer as a regular user.
- Try to create a custom event log by using Visual Studio .NET, and then try to write to this event log by using an application that is built on Visual Studio .NET.
source :
http://support.microsoft.com/kb/307024
http://support.microsoft.com/kb/842795

1 komentar:
dirty obama nigger lover
Posting Komentar