Applied patch from Jan Limpens 'ReflectionBasedDictionaryAdapter needs to check if...
[castle.git] / Core / Castle.Core / Logging / Loggers / DiagnosticsLogger.cs
blob600560193f725108beb1a9e4a91aa8acb1057733
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Core.Logging
17 #if !SILVERLIGHT
19 using System;
20 using System.Diagnostics;
22 /// <summary>
23 /// The Logger using standart Diagnostics namespace.
24 /// </summary>
25 [Serializable]
26 public class DiagnosticsLogger : LevelFilteredLogger, IDisposable
28 private EventLog eventLog;
30 /// <summary>
31 /// Creates a logger based on <see cref="System.Diagnostics.EventLog"/>.
32 /// </summary>
33 /// <param name="logName"><see cref="EventLog.Log"/></param>
34 public DiagnosticsLogger(string logName) : this(logName, "default")
38 /// <summary>
39 /// Creates a logger based on <see cref="System.Diagnostics.EventLog"/>.
40 /// </summary>
41 /// <param name="logName"><see cref="EventLog.Log"/></param>
42 /// <param name="source"><see cref="EventLog.Source"/></param>
43 public DiagnosticsLogger(string logName, string source) : base(LoggerLevel.Debug)
45 // Create the source, if it does not already exist.
46 if (!EventLog.SourceExists(source))
48 EventLog.CreateEventSource(source, logName);
51 eventLog = new EventLog(logName);
52 eventLog.Source = source;
55 /// <summary>
56 /// Creates a logger based on <see cref="System.Diagnostics.EventLog"/>.
57 /// </summary>
58 /// <param name="logName"><see cref="EventLog.Log"/></param>
59 /// <param name="machineName"><see cref="EventLog.MachineName"/></param>
60 /// <param name="source"><see cref="EventLog.Source"/></param>
61 public DiagnosticsLogger(string logName, string machineName, string source)
63 // Create the source, if it does not already exist.
64 if (!EventLog.SourceExists(source, machineName))
66 EventSourceCreationData eventSourceCreationData = new EventSourceCreationData(source, logName);
67 eventSourceCreationData.MachineName = machineName;
68 EventLog.CreateEventSource(eventSourceCreationData);
71 eventLog = new EventLog(logName, machineName, source);
74 ~DiagnosticsLogger()
76 Close(false);
79 #region IDisposable Members
81 public void Dispose()
83 Close(true);
86 #endregion
88 public void Close()
90 Close(true);
93 protected void Close(bool supressFinalize)
95 if (supressFinalize)
97 GC.SuppressFinalize(this);
100 if (eventLog != null)
102 eventLog.Close();
103 eventLog = null;
107 public override ILogger CreateChildLogger(string newName)
109 return new DiagnosticsLogger(eventLog.Log, eventLog.MachineName, eventLog.Source);
112 protected override void Log(LoggerLevel level, string name, string message, Exception exception)
114 if (eventLog == null) return; // just in case it was disposed
116 EventLogEntryType type = TranslateLevel(level);
118 String contentToLog;
120 if (exception == null)
122 contentToLog = string.Format("[{0}] '{1}' message: {2}", level.ToString(), name, message);
124 else
126 contentToLog = string.Format("[{0}] '{1}' message: {2} exception: {3} {4} {5}",
127 level.ToString(), name, message, exception.GetType(), exception.Message,
128 exception.StackTrace);
131 eventLog.WriteEntry(contentToLog, type);
134 private EventLogEntryType TranslateLevel(LoggerLevel level)
136 switch(level)
138 case LoggerLevel.Error:
139 case LoggerLevel.Fatal:
140 return EventLogEntryType.Error;
141 case LoggerLevel.Warn:
142 return EventLogEntryType.Warning;
143 default:
144 return EventLogEntryType.Information;
148 #endif