1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
20 using System
.Diagnostics
;
23 /// The Logger using standart Diagnostics namespace.
26 public class DiagnosticsLogger
: LevelFilteredLogger
, IDisposable
28 private EventLog eventLog
;
31 /// Creates a logger based on <see cref="System.Diagnostics.EventLog"/>.
33 /// <param name="logName"><see cref="EventLog.Log"/></param>
34 public DiagnosticsLogger(string logName
) : this(logName
, "default")
39 /// Creates a logger based on <see cref="System.Diagnostics.EventLog"/>.
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
;
56 /// Creates a logger based on <see cref="System.Diagnostics.EventLog"/>.
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
);
79 #region IDisposable Members
93 protected void Close(bool supressFinalize
)
97 GC
.SuppressFinalize(this);
100 if (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
);
120 if (exception
== null)
122 contentToLog
= string.Format("[{0}] '{1}' message: {2}", level
.ToString(), name
, message
);
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
)
138 case LoggerLevel
.Error
:
139 case LoggerLevel
.Fatal
:
140 return EventLogEntryType
.Error
;
141 case LoggerLevel
.Warn
:
142 return EventLogEntryType
.Warning
;
144 return EventLogEntryType
.Information
;