c# - Event Creation is not working -


i have source "source401" used log "log401". need use source "log402" log , delete log "log401". (if can rename “log401” “log402” fine. need done programmatically)

with code below, getting following exception. best way achieve it?

source source401 exists on local computer.

note: when delete old log, working fine. events not getting created.

update

from msdn

the operating system stores event logs files. when use eventloginstaller or createeventsource create new event log, associated file stored in %systemroot%\system32\config directory on specified computer. file name set appending first 8 characters of log property ".evt" file name extension.

the source must unique on local computer; new source name cannot match existing source name or existing event log name. each source can write 1 event log @ time; however, application can use multiple sources write multiple event logs.

code

  string source = "source401";   string logname = "log402";   string oldlogname = "log401";   string eventname = "sample event";   string machinename = ".";               if (!eventlog.exists(logname, machinename))             {                 ////delete old log                 //if (eventlog.exists(oldlogname, machinename))                 //{                 //    eventlog.delete(oldlogname, machinename);                 //}                    //create source log                 eventlog.createeventsource(source, logname, machinename);                  //create event                 eventlog eventlog = new eventlog(logname, machinename, source);                 eventlog.writeentry(eventname);                 try                 {                     eventlog.writeentry(eventname, eventlogentrytype.warning, 234, (short)3);                 }                 catch (exception exception)                 {                     int x = 0;                 } 

the exception telling exactly problem is. event source named "source401" exists. you're deleting old event log, "log401", you're not deleting event source.

as the documentation says:

the operating system stores event logs files. when use eventloginstaller or createeventsource create new event log, associated file stored in %systemroot%\system32\config directory on specified computer. file name set appending first 8 characters of log property ".evt" file name extension.

the source must unique on local computer; new source name cannot match existing source name or existing event log name. each source can write 1 event log @ time;

also, little nugget:

if source has been mapped log , remap new log, must restart computer changes take effect.

in addition, might want consider this, documentation:

create new event source during installation of application. allows time operating system refresh list of registered event sources , configuration. if operating system has not refreshed list of event sources, , attempt write event new source, write operation fail

finally, createeventsource method you're calling marked obsolete, , has been since .net 2.0. there's reason methods marked obsolete. should calling createeventsource(eventsourcecreationdata).

i think need re-think way you're using event logs. application shouldn't creating , deleting logs way. it's not how they're intended used.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -