Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EventLog WriteEntry does not write to designated log, but to Application log

Tags:

c#

event-log

I have an application where I want to write entries to event log The logger is instantiated through MEF. I created a derived class, to be able to perform the log initializations prior of using it.

My code is as below:

public class WinEventLog : EventLog, ILogger
{
    private const string LOG_SourceName = "DataGen_Source";
    private const string LOG_SysLogName = "Pool_Log";

    private bool _isInitialized = false;

    public WinEventLog()
        : base()
    {
        Initialize();
    }

    public void LogMessage(MessageLevel level, string message)
    {
        WriteEntry(message, level.EventLogType());
    }

    public void LogMessage(string source, MessageLevel level, string message)
    {
        WriteEntry(source, message, level.EventLogType());
    }

    public void Initialize()
    {
        if (!_isInitialized)
        {
            this.BeginInit();
            this.EndInit();

            if (!System.Diagnostics.EventLog.SourceExists(LOG_SourceName))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    LOG_SourceName, LOG_SysLogName);
            }
            Source = LOG_SourceName;
            Log = LOG_SysLogName;
            _isInitialized = true;
        }
    }
} 

However, the logger does not write into the log I specify, Pool_Log, but in Applications log.

Any idea why this happens?


EDIT

I referenced EXACT the same component from other project, and in this situation it wrote to the correct EventLog !!!

I'm puzzled!

Thanks

like image 884
bzamfir Avatar asked Oct 09 '22 15:10

bzamfir


2 Answers

it appears that you are creating the source but not creating the actual Log for example I would do something like the following if I wanted to create a SQLEventLog

public bool CreateLog(string strLogName)
{
    bool Result = false;

    try
    {
            System.Diagnostics.EventLog.CreateEventSource(strLogName, strLogName);
            System.Diagnostics.EventLog SQLEventLog = 
            new System.Diagnostics.EventLog();

            SQLEventLog.Source = strLogName;
            SQLEventLog.Log = strLogName;

            SQLEventLog.Source = strLogName;
            SQLEventLog.WriteEntry("The " + strLogName + " was successfully 
        initialize component.", EventLogEntryType.Information);

            Result = true;
    }
    catch
    {
        Result = false;
    }

    return Result;
}

let me know if this helps.. looks like you are missing the EventLog Creation

like image 100
MethodMan Avatar answered Oct 11 '22 05:10

MethodMan


you can try the following :

evntSource = "MySource";

evntLog = "MyLog"; //This replace Application!
evntEvent = "My Event";
if (!EventLog.SourceExists(evntSource))
    EventLog.CreateEventSource(evntSource,evntLog); 

EventLog.WriteEntry(evntSource,evntEvent);
like image 22
Giorgio Minardi Avatar answered Oct 11 '22 05:10

Giorgio Minardi