Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Logger.cs @ 6765

Last change on this file since 6765 was 6743, checked in by ascheibe, 13 years ago

#1233

  • fixed a bug in the Slave UI
  • finished renaming Webservice and Dao methods to be consistent with Job/Task naming
  • some cosmetic changes and project dependencies cleanups
File size: 2.5 KB
RevLine 
[6431]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;
[5511]23using System.Diagnostics;
[5593]24using System.Security;
25using System.Threading;
[5511]26
[6717]27namespace HeuristicLab.Services.Hive.DataAccess {
[5511]28  public class LogFactory {
29    public static ILogger GetLogger(string source) {
[6743]30      return new Logger(Settings.Default.EventLogName, source);
[5511]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
[5593]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>
[5511]47    public Logger(string name, string source) {
[5593]48      try {
49        if (!EventLog.SourceExists(source)) {
50          EventLog.CreateEventSource(source, name);
51        }
52        log = new EventLog(name);
53        log.Source = source;
54      }
55      catch (SecurityException) { }
[5511]56    }
57
58    public void Log(string message) {
[5593]59      try {
[5711]60        if (log != null) {
61          log.WriteEntry(string.Format("{0} (AppDomain: {1}, Thread: {2})", message, AppDomain.CurrentDomain.Id, Thread.CurrentThread.ManagedThreadId), EventLogEntryType.Information);
62        }
[5593]63      }
64      catch (SecurityException) { }
[5511]65    }
66
67    public void Error(Exception e) {
[5593]68      try {
[5711]69        if (log != null) {
70          log.WriteEntry(string.Format("{0} (AppDomain: {1}, Thread: {2})", e.Message, AppDomain.CurrentDomain.Id, Thread.CurrentThread.ManagedThreadId), EventLogEntryType.Error);
71        }
[5593]72      }
73      catch (SecurityException) { }
[5511]74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.