Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Common/3.4/Logger.cs @ 5593

Last change on this file since 5593 was 5593, checked in by cneumuel, 14 years ago

#1233

  • changed the way lifecycle methods are called. The new service method TriggerLifecycle checks when the latest cleanup was made and performs one (if necessary). This can also be called by an external program (like a windows task)
  • robustified logging
File size: 1.6 KB
Line 
1using System;
2using System.Diagnostics;
3using System.Security;
4using System.Threading;
5
6namespace HeuristicLab.Services.Hive.Common {
7  public class LogFactory {
8    public static ILogger GetLogger(string source) {
9      return new Logger("HeuristicLab.Hive", source);
10    }
11  }
12
13  public interface ILogger {
14    void Log(string message);
15    void Error(Exception e);
16  }
17
18  internal class Logger : ILogger {
19    private EventLog log;
20
21    /// <summary>
22    /// Creating an EventSource requires certain permissions, which by default a IIS AppPool user does not have.
23    /// In this case just ignore security exceptions.
24    /// In order to make this work, the eventsource needs to be created manually
25    /// </summary>
26    public Logger(string name, string source) {
27      try {
28        if (!EventLog.SourceExists(source)) {
29          EventLog.CreateEventSource(source, name);
30        }
31        log = new EventLog(name);
32        log.Source = source;
33      }
34      catch (SecurityException) { }
35    }
36
37    public void Log(string message) {
38      try {
39        log.WriteEntry(string.Format("{0} (AppDomain: {1}, Thread: {2})", message, AppDomain.CurrentDomain.Id, Thread.CurrentThread.ManagedThreadId), EventLogEntryType.Information);
40      }
41      catch (SecurityException) { }
42    }
43
44    public void Error(Exception e) {
45      try {
46        log.WriteEntry(string.Format("{0} (AppDomain: {1}, Thread: {2})", e.Message, AppDomain.CurrentDomain.Id, Thread.CurrentThread.ManagedThreadId), EventLogEntryType.Error);
47      }
48      catch (SecurityException) { }
49    }
50  }
51}
Note: See TracBrowser for help on using the repository browser.