using System; using System.Diagnostics; using System.Security; using System.Threading; namespace HeuristicLab.Services.Hive.Common { public class LogFactory { public static ILogger GetLogger(string source) { return new Logger("HL.Hive", source); } } public interface ILogger { void Log(string message); void Error(Exception e); } internal class Logger : ILogger { private EventLog log; /// /// Creating an EventSource requires certain permissions, which by default a IIS AppPool user does not have. /// In this case just ignore security exceptions. /// In order to make this work, the eventsource needs to be created manually /// public Logger(string name, string source) { try { if (!EventLog.SourceExists(source)) { EventLog.CreateEventSource(source, name); } log = new EventLog(name); log.Source = source; } catch (SecurityException) { } } public void Log(string message) { try { if (log != null) { log.WriteEntry(string.Format("{0} (AppDomain: {1}, Thread: {2})", message, AppDomain.CurrentDomain.Id, Thread.CurrentThread.ManagedThreadId), EventLogEntryType.Information); } } catch (SecurityException) { } } public void Error(Exception e) { try { if (log != null) { log.WriteEntry(string.Format("{0} (AppDomain: {1}, Thread: {2})", e.Message, AppDomain.CurrentDomain.Id, Thread.CurrentThread.ManagedThreadId), EventLogEntryType.Error); } } catch (SecurityException) { } } } }