Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Logger.cs @ 7157

Last change on this file since 7157 was 7157, checked in by ascheibe, 12 years ago

#1672 improved event logging in the Hive service

File size: 2.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Diagnostics;
24using System.Security;
25using System.Threading;
26
27namespace HeuristicLab.Services.Hive.DataAccess {
28  public class LogFactory {
29    public static ILogger GetLogger(string source) {
30      return new Logger(Settings.Default.EventLogName, source);
31    }
32  }
33
34  public interface ILogger {
35    void Log(string message);
36    void Error(Exception e);
37  }
38
39  internal class Logger : ILogger {
40    private EventLog log;
41
42    /// <summary>
43    /// Creating an EventSource requires certain permissions, which by default a IIS AppPool user does not have.
44    /// In this case just ignore security exceptions.
45    /// In order to make this work, the eventsource needs to be created manually
46    /// </summary>
47    public Logger(string name, string source) {
48      try {
49        log = new EventLog(name);
50        log.Source = source;
51      }
52      catch (Exception) { }
53    }
54
55    public void Log(string message) {
56      try {
57        if (log != null) {
58          log.WriteEntry(string.Format("{0} (AppDomain: {1}, Thread: {2})", message, AppDomain.CurrentDomain.Id, Thread.CurrentThread.ManagedThreadId), EventLogEntryType.Information);
59        }
60      }
61      catch (Exception) { }
62    }
63
64    public void Error(Exception e) {
65      try {
66        if (log != null) {
67          log.WriteEntry(string.Format("{0} (AppDomain: {1}, Thread: {2})", e.Message, AppDomain.CurrentDomain.Id, Thread.CurrentThread.ManagedThreadId), EventLogEntryType.Error);
68        }
69      }
70      catch (Exception) { }
71    }
72  }
73}
Note: See TracBrowser for help on using the repository browser.