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.
Erwin PombettAsked:
Who is Participating?
Look at this url.

Note: I am starting to think, that you cannot write logs if you are with the account, unless you give the privileges, this topic is really interesting, I will dig into it.
sorry, got the bad url. is this
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Erwin PombettAuthor 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

Erwin PombettAuthor 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)
Erwin PombettAuthor 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.
Erwin PombettAuthor 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)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.