Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

how can i log events to myLog?

hello experts,

when i run "eventvwr.msc" i can see that i created a log called "MyLog".
now when i try to write logs to it , my logs are written to a log called "Application".

in order to write to my log i try the following code in the begining of my service.

                EventLog.CreateEventSource("drains", "MyLog");
            aDrainsLog_ = new EventLog();
            aDrainsLog_.Source = "drains";

i know that i could use a static method to write evetns.

what would be my test in order to check and then to write to it using the
static method.

any help is welcome.
  • 6
  • 4
1 Solution
sorry, got the bad url. is this

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

toshi_Author Commented:
thank ingfraga,

thank you for the links, i changed my methods and tested both,
static and instance,
then the problem still the same, i can not write to the log i want,
everything out of my window service finished in the "Application" in event viewer.

i catch the error and it inform me  than my source is not legistered in the log i want to write in.
how can pass over this problem?

thank you in advance.
you could try, calling win32 api, wich I really dont like, but here is it.
// DLL import for backing up an event log
[DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern bool BackupEventLog(IntPtr hEventLog, string backupFile);
// DLL import for opening an event log
[DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern IntPtr OpenEventLog(string lpUNCServerName, string lpSourceName);
// Build the back-up filename (full path)
string EventFile = string.Format("{0}\\Log-{1}-{2}-{3}-{4:T}.evt", Globals.RunningDirectory,DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now);
// Open the log for backing up
IntPtr LogHandle = OpenEventLog(ProgramEventLog.MachineName, LogName);
// Backup the log to the specified filename
BackupEventLog(LogHandle, EventFile);

Open in new window

toshi_Author Commented:
can you explain me what this code is aabout,

what does it mean "backing up" ? does it save the event log ?
i want only to write to the file thats all.
how do i set lpUNCServerName? lpSourceName...
why all this Format file to Log-2009-..evt? my file is called "MyLog", do i change that?
need an explanation;o)  thank in advance.
Again sorry, that wasnt the code I would like you to see!
I am doing so many thing! my god!


( please delete this 2 messages)
toshi_Author Commented:
when i debug:
1.            if(!EventLog.SourceExists("drains"))
2.                EventLog.CreateEventSource("drains", "eplan_services");
3.            aDrainsLog_ = new EventLog("eplan_services","SHIRO-2", "drains");
4.            aDrainsLog_.Source = "drains";

line 2,  can see that aDrainsLog_ has Log = Application,
line 3 , "Log" change to eplan_service,  

but later in other methods, when writing to aDrainsLog_ it would write to Application event.
...i dont understand.
toshi_Author Commented:
....mdr .....
there must be impressive to see you working, answering to questions, differents threads at the same time,
i dont know for how many subjets your're experts but some are so impressive ;o)

Look at this url.


Note: I am starting to think, that you cannot write logs if you are with the asp.net account, unless you give the privileges, this topic is really interesting, I will dig into it.

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now