Question

.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);
            }
        } 
    }
}

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top