[5511] | 1 | using System;
|
---|
| 2 | using System.Diagnostics;
|
---|
[5593] | 3 | using System.Security;
|
---|
| 4 | using System.Threading;
|
---|
[5511] | 5 |
|
---|
| 6 | namespace HeuristicLab.Services.Hive.Common {
|
---|
| 7 | public class LogFactory {
|
---|
| 8 | public static ILogger GetLogger(string source) {
|
---|
[5711] | 9 | return new Logger("HL.Hive", source);
|
---|
[5511] | 10 | }
|
---|
| 11 | }
|
---|
| 12 |
|
---|
| 13 | public interface ILogger {
|
---|
| 14 | void Log(string message);
|
---|
| 15 | void Error(Exception e);
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | internal class Logger : ILogger {
|
---|
| 19 | private EventLog log;
|
---|
| 20 |
|
---|
[5593] | 21 | /// <summary>
|
---|
| 22 | /// Creating an EventSource requires certain permissions, which by default a IIS AppPool user does not have.
|
---|
| 23 | /// In this case just ignore security exceptions.
|
---|
| 24 | /// In order to make this work, the eventsource needs to be created manually
|
---|
| 25 | /// </summary>
|
---|
[5511] | 26 | public Logger(string name, string source) {
|
---|
[5593] | 27 | try {
|
---|
| 28 | if (!EventLog.SourceExists(source)) {
|
---|
| 29 | EventLog.CreateEventSource(source, name);
|
---|
| 30 | }
|
---|
| 31 | log = new EventLog(name);
|
---|
| 32 | log.Source = source;
|
---|
| 33 | }
|
---|
| 34 | catch (SecurityException) { }
|
---|
[5511] | 35 | }
|
---|
| 36 |
|
---|
| 37 | public void Log(string message) {
|
---|
[5593] | 38 | try {
|
---|
[5711] | 39 | if (log != null) {
|
---|
| 40 | log.WriteEntry(string.Format("{0} (AppDomain: {1}, Thread: {2})", message, AppDomain.CurrentDomain.Id, Thread.CurrentThread.ManagedThreadId), EventLogEntryType.Information);
|
---|
| 41 | }
|
---|
[5593] | 42 | }
|
---|
| 43 | catch (SecurityException) { }
|
---|
[5511] | 44 | }
|
---|
| 45 |
|
---|
| 46 | public void Error(Exception e) {
|
---|
[5593] | 47 | try {
|
---|
[5711] | 48 | if (log != null) {
|
---|
| 49 | log.WriteEntry(string.Format("{0} (AppDomain: {1}, Thread: {2})", e.Message, AppDomain.CurrentDomain.Id, Thread.CurrentThread.ManagedThreadId), EventLogEntryType.Error);
|
---|
| 50 | }
|
---|
[5593] | 51 | }
|
---|
| 52 | catch (SecurityException) { }
|
---|
[5511] | 53 | }
|
---|
| 54 | }
|
---|
| 55 | } |
---|