Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2865-Tourplanning/HeuristicLab.Services.Hive.DataAccess/3.3/Logger.cs @ 15660

Last change on this file since 15660 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

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