0

.Net4.0, Windows 7(64bit), C# Winform based application. Here is the method writing event log. However, there isn't shown event log on MS Eventviewer. Do you have any idea ?

private static void WriteEventLogEntry(string message)
{
        // Create an instance of EventLog
        System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog();

        // Check if the event source exists. If not create it.
        if (!System.Diagnostics.EventLog.SourceExists("TestProgram"))
        {
            System.Diagnostics.EventLog.CreateEventSource("TestProgram", "Application");
        }

        // Set the source name for writing log entries.
        eventLog.Source = "TestProgram";

        // Create an event ID to add to the event log
        int eventID = 8;

        // Write an entry to the event log.
        eventLog.WriteEntry(message,
                            System.Diagnostics.EventLogEntryType.Information,
                            eventID);

        // Close the Event Log
        eventLog.Close();
    } 
}

EDIT:

I have changed the code below. I can see event log without running as administrator. I have one more question. There are two log such as "Log 1" and "Log 2". I can see "Log 1" on viewer. However, I can't see "Log 2". Do you have any idea ?

using System.Diagnostics;

namespace ImageDelegateSample
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            MdiForm mainForm = null;
            try
            {                    
                  bool createdNew;
                Mutex dup = new Mutex(true, "ImageDelegateSample", out createdNew);
                if (createdNew)
                {
                    // Log 1
                    WriteEventLogEntry("Loading", EventLogEntryType.Information);

                    mainForm = new MdiForm();
                    Application.Run(mainForm);    

                    // Log 2
                    WriteEventLogEntry("Done Loading", EventLogEntryType.Information);

                }
                else
                {
                    MessageBox.Show("already running.");
                }
            }
            catch (Exception e)
            {
                // You probably want something a little more sophisticated than this
                MessageBox.Show(e.Message, "An exception occurred:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                HandleError(e);

                System.Environment.Exit(0);
            }
        }

        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            HandleError(e.Exception);
        }

        internal static void HandleError(Exception e)
        {            
            string message = e.Message + "\n\n";
            if (e.InnerException != null)
                message += "Inner exception:\n" + e.InnerException.Message + "\n\n";
            message += e.StackTrace;
            MessageBox.Show(message, "An error has occurred:");
        }

        private static readonly string EventLogName = "Application";
        private static readonly string EventLogSource = "ImageDelegateSample.exe";

        private static void WriteEventLogEntry(string message, EventLogEntryType type)
        {
            // In the only function that does something
            if (!EventLog.Exists(EventLogName))
            {
                EventLog.CreateEventSource(EventLogSource, EventLogName);
                return;
            }
            else
            {
                // This doesn't write anything
                EventLog.WriteEntry(EventLogSource,
                    message,
                    type);
            }
        } 
    }
}
Changju.rhee
  • 463
  • 3
  • 11
  • 26
  • 3
    Have you tried running your program as an administrator at least once? You need elevated permissions to create a new eventlog. – Mark Hall Jan 26 '14 at 05:07
  • It is also good idea to check if there are any errors/exception from this code... – Alexei Levenkov Jan 26 '14 at 05:08
  • 1
    you would get an exception if you didn't have the creds to write to the event viewer. – T McKeown Jan 26 '14 at 05:10
  • @TMcKeown That is true, but we don't know if he has any Try/Catch logic around the call of this method. – Mark Hall Jan 26 '14 at 05:12
  • @TMcKeown What is the creds ? How to make a creds ? – Changju.rhee Jan 26 '14 at 06:04
  • 1
    credentials... run the application as administrator... or if you are testing within VS run as admin there. Right click on the shortcut, click Run as Administrator. Of course you must have local admin priv to do so in the first place. – T McKeown Jan 26 '14 at 06:06
  • You can also take a look at this. May be you might find some help here. http://stackoverflow.com/questions/804284/how-do-i-write-to-a-custom-windows-event-log?rq=1 – Prajul G Jan 26 '14 at 06:16
  • @PrajulG thanks for your help. It works without running application as administrator. – Changju.rhee Jan 26 '14 at 06:44
  • @PrajulG I have edited my question. Do you have any idea for another question ? – Changju.rhee Jan 26 '14 at 07:01

0 Answers0