Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Services.Hive.DataAccess/3.3/Logger.cs @ 15601

Last change on this file since 15601 was 14712, checked in by gkronber, 8 years ago

#2520 added GUIDs for (almost) all interface types (probably still too many) also added newlines at end of all files

File size: 2.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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      //ignore the source parameter for now
31      return new Logger(source, Settings.Default.EventLogName);
32    }
33  }
34
35  [HeuristicLab.Persistence.Default.CompositeSerializers.Storable.StorableType("B9F092DB-1F4C-4B85-8EC2-4C893C627CBD")]
36  public interface ILogger {
37    void Log(string message);
38    void Error(Exception e);
39  }
40
41  internal class Logger : ILogger {
42    private EventLog log;
43
44    /// <summary>
45    /// Creating an EventSource requires certain permissions, which by default a IIS AppPool user does not have.
46    /// In this case just ignore security exceptions.
47    /// In order to make this work, the eventsource needs to be created manually
48    /// </summary>
49    public Logger(string name, string source) {
50      try {
51        log = new EventLog();
52        log.Source = source;
53      }
54      catch (Exception) { }
55    }
56
57    public void Log(string message) {
58      try {
59        if (log != null) {
60          log.WriteEntry(string.Format("{0} (AppDomain: {1}, Thread: {2})", message, AppDomain.CurrentDomain.Id, Thread.CurrentThread.ManagedThreadId), EventLogEntryType.Information);
61        }
62      }
63      catch (Exception) { }
64    }
65
66    public void Error(Exception e) {
67      try {
68        if (log != null) {
69          log.WriteEntry(string.Format("{0} (AppDomain: {1}, Thread: {2})", e.Message, AppDomain.CurrentDomain.Id, Thread.CurrentThread.ManagedThreadId), EventLogEntryType.Error);
70        }
71      }
72      catch (Exception) { }
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.