开发者

creating a place for and writing events to a custom area under "Applications and Services Logs"

开发者 https://www.devze.com 2023-03-16 02:30 出处:网络
In theWindows 7 and Server 2008 Event Viewer there is a folder for... Applications and S开发者_Go百科ervices Logs

In the Windows 7 and Server 2008 Event Viewer there is a folder for...

Applications and S开发者_Go百科ervices Logs

I want to create a place where all events from my application are logged. How do you create a log under this area? From the event viewer, all I see is "Create View" which appears to simply be a way to create a filtered view of events in a particular log.

Once this is created, how do you write events to it?

For example if my log is named 'StackApp', would you just use...

EventLog.WriteEntry('StackApp', message);

Thanks!


if your EventLog was created then you can do EventLog.WriteEntry("StackApp", message);

//create a log
string source;
string logName;
string machineName;//use "." for this machine

if (!EventLog.Exists(logName, machineName))
{
    EventSourceCreationData creationData = new EventSourceCreationData(source, name);
    EventLog.CreateEventSource(creationData);
}

More operation on log:

//find your log in the windows EventLog by name
EventLog log = null;
foreach (EventLog eventLog in EventLog.GetEventLogs())
{
    if (string.Compare(eventLog.Log, logName, true) == 0)
    {
        log = eventLog;
        break;
    }
}

//modify log settings
log.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 7);
log.MaximumKilobytes = MaxLogSize;

//write to event to log
EventLog.WriteEntry(source, message);

//more advance writing to log instance
long instanceId;
int categoryId;;
EventLogEntryType entryType;
byte[] binaryData;
object[] values;

EventInstance eventInstance = new EventInstance(instanceId, categoryId, entryType);
log.WriteEvent(eventInstance, binaryData, values);
0

精彩评论

暂无评论...
验证码 换一张
取 消