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 |
|
---|
22 | using System;
|
---|
23 | using System.Diagnostics;
|
---|
24 | using System.Security;
|
---|
25 | using System.Threading;
|
---|
26 |
|
---|
27 | namespace 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 | if (!EventLog.SourceExists(source)) {
|
---|
50 | EventLog.CreateEventSource(source, name);
|
---|
51 | }
|
---|
52 | log = new EventLog(name);
|
---|
53 | log.Source = source;
|
---|
54 | }
|
---|
55 | catch (SecurityException) { }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public void Log(string message) {
|
---|
59 | try {
|
---|
60 | if (log != null) {
|
---|
61 | log.WriteEntry(string.Format("{0} (AppDomain: {1}, Thread: {2})", message, AppDomain.CurrentDomain.Id, Thread.CurrentThread.ManagedThreadId), EventLogEntryType.Information);
|
---|
62 | }
|
---|
63 | }
|
---|
64 | catch (SecurityException) { }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public void Error(Exception e) {
|
---|
68 | try {
|
---|
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 | }
|
---|
72 | }
|
---|
73 | catch (SecurityException) { }
|
---|
74 | }
|
---|
75 | }
|
---|
76 | } |
---|