[6983] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15973] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6983] | 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) {
|
---|
[7158] | 30 | //ignore the source parameter for now
|
---|
| 31 | return new Logger(source, Settings.Default.EventLogName);
|
---|
[6983] | 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 {
|
---|
[7158] | 50 | log = new EventLog();
|
---|
[6983] | 51 | log.Source = source;
|
---|
| 52 | }
|
---|
[7157] | 53 | catch (Exception) { }
|
---|
[6983] | 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 | }
|
---|
[7157] | 62 | catch (Exception) { }
|
---|
[6983] | 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 | }
|
---|
[7157] | 71 | catch (Exception) { }
|
---|
[6983] | 72 | }
|
---|
| 73 | }
|
---|
| 74 | } |
---|