3 // Atsushi Enomoto <atsushi@ximian.com>
5 // Copyright (C) 2011 Novell, Inc. http://www.novell.com
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 using System.Collections.Generic;
28 using System.Configuration;
29 using System.Diagnostics;
31 using System.ServiceModel;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Configuration;
34 using System.ServiceModel.Diagnostics;
35 using System.Threading;
38 using System.Xml.XPath;
41 namespace System.ServiceModel
43 internal enum MessageLogSourceKind
47 ServiceLevelReceiveDatagram,
48 ServiceLevelSendDatagram,
49 // more, maybe for clients?
52 internal static class Logger
55 enum TraceEventType // dummy
70 const string xmlns = "http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace";
71 static MessageLoggingSettings settings = new MessageLoggingSettings ();
73 static TextWriter log_writer = TextWriter.Null;
74 static XmlWriter xml_writer;
76 static readonly TraceSource source = new TraceSource ("System.ServiceModel");
77 static readonly TraceSource message_source = new TraceSource ("System.ServiceModel.MessageLogging");
83 Environment.GetEnvironmentVariable ("MOON_WCF_TRACE") ??
84 Environment.GetEnvironmentVariable ("MONO_WCF_TRACE");
88 log_writer = Console.Out;
91 log_writer = Console.Error;
96 if (!String.IsNullOrEmpty (env))
97 log_writer = File.CreateText (env);
98 } catch (Exception ex) {
99 Console.Error.WriteLine ("WARNING: WCF trace environment variable points to non-creatable file name: " + env);
104 xml_writer = XmlWriter.Create (log_writer, new XmlWriterSettings () { OmitXmlDeclaration = true });
107 message_source.Switch.Level = SourceLevels.Information;
111 #region logger methods
113 public static void Critical (string message, params object [] args)
115 Log (TraceEventType.Critical, message, args);
118 public static void Error (string message, params object [] args)
120 Log (TraceEventType.Error, message, args);
123 public static void Warning (string message, params object [] args)
125 Log (TraceEventType.Warning, message, args);
128 public static void Info (string message, params object [] args)
130 Log (TraceEventType.Information, message, args);
133 public static void Verbose (string message, params object [] args)
135 Log (TraceEventType.Verbose, message, args);
138 // FIXME: do we need more?
140 static void Log (TraceEventType eventType, string message, params object [] args)
144 log_writer.Write ("[{0}] ", event_id);
146 TraceCore (TraceEventType.Information, event_id,
147 false, Guid.Empty, // FIXME
149 log_writer.WriteLine (message, args);
152 source.TraceEvent (eventType, event_id, message, args);
158 #region message logging
160 static readonly XmlWriterSettings xws = new XmlWriterSettings () { OmitXmlDeclaration = true };
162 public static void LogMessage (MessageLogSourceKind sourceKind, ref Message msg, long maxMessageSize)
164 if (maxMessageSize > int.MaxValue)
165 throw new ArgumentOutOfRangeException ("maxMessageSize");
166 var mb = msg.CreateBufferedCopy ((int) maxMessageSize);
167 msg = mb.CreateMessage ();
168 LogMessage (new MessageLogTraceRecord (sourceKind, msg.GetType (), mb));
171 public static void LogMessage (MessageLogTraceRecord log)
173 var sw = new StringWriter ();
175 var xw = XmlWriter.Create (sw, xws);
177 var doc = new XmlDocument ();
178 var xw = doc.CreateNavigator ().AppendChild ();
180 xw.WriteStartElement ("MessageLogTraceRecord", xmlns);
181 xw.WriteStartAttribute ("Time");
182 xw.WriteValue (log.Time);
183 xw.WriteEndAttribute ();
184 xw.WriteAttributeString ("Source", log.Source.ToString ());
185 xw.WriteAttributeString ("Type", log.Type.FullName);
186 var msg = log.Message.CreateMessage ();
188 msg.WriteMessage (xw);
189 xw.WriteEndElement ();
195 log_writer.Write ("[{0}] ", event_id);
197 TraceCore (TraceEventType.Information, event_id, /*FIXME*/false, /*FIXME*/Guid.Empty, sw);
199 TraceCore (TraceEventType.Information, event_id, /*FIXME*/false, /*FIXME*/Guid.Empty, doc.CreateNavigator ());
201 message_source.TraceData (TraceEventType.Information, event_id, doc.CreateNavigator ());
209 #region XmlWriterTraceListener compatibility
210 static void TraceCore (//TraceEventCache eventCache,
211 /*string source,*/ TraceEventType eventType, int id,
212 bool hasRelatedActivity, Guid relatedActivity,
213 /*int level, bool wrapData, */params object [] data)
215 string source = "mono(dummy)";
217 bool wrapData = true;
220 w.WriteStartElement ("E2ETraceEvent", e2e_ns);
223 w.WriteStartElement ("System", sys_ns);
224 w.WriteStartElement ("EventID", sys_ns);
225 w.WriteString (XmlConvert.ToString (id));
226 w.WriteEndElement ();
227 w.WriteStartElement ("Type", sys_ns);
228 // ...what to write here?
230 w.WriteEndElement ();
231 w.WriteStartElement ("SubType", sys_ns);
232 // FIXME: it does not seem always to match eventType value ...
233 w.WriteAttributeString ("Name", eventType.ToString ());
234 // ...what to write here?
236 w.WriteEndElement ();
237 // ...what to write here?
238 w.WriteStartElement ("Level", sys_ns);
239 w.WriteString (level.ToString ());
240 w.WriteEndElement ();
241 w.WriteStartElement ("TimeCreated", sys_ns);
242 w.WriteAttributeString ("SystemTime", XmlConvert.ToString (DateTime.Now, XmlDateTimeSerializationMode.RoundtripKind));
243 w.WriteEndElement ();
244 w.WriteStartElement ("Source", sys_ns);
245 w.WriteAttributeString ("Name", source);
246 w.WriteEndElement ();
247 w.WriteStartElement ("Correlation", sys_ns);
248 w.WriteAttributeString ("ActivityID", String.Concat ("{", Guid.Empty, "}"));
249 w.WriteEndElement ();
250 w.WriteStartElement ("Execution", sys_ns);
251 w.WriteAttributeString ("ProcessName", "mono (dummy)");
252 w.WriteAttributeString ("ProcessID", "0");
253 w.WriteAttributeString ("ThreadID", Thread.CurrentThread.ManagedThreadId.ToString ());
254 w.WriteEndElement ();
255 w.WriteStartElement ("Channel", sys_ns);
256 // ...what to write here?
257 w.WriteEndElement ();
258 w.WriteStartElement ("Computer");
259 w.WriteString ("localhost(dummy)");
260 w.WriteEndElement ();
262 w.WriteEndElement ();
265 w.WriteStartElement ("ApplicationData", e2e_ns);
266 w.WriteStartElement ("TraceData", e2e_ns);
267 foreach (object o in data) {
269 w.WriteStartElement ("DataItem", e2e_ns);
271 // in moonlight we don't have XPathNavigator, so just use raw string...
273 w.WriteString (o.ToString ());
275 if (o is XPathNavigator)
276 // the output ignores xmlns difference between the parent (E2ETraceEvent and the content node).
277 // To clone such behavior, I took this approach.
278 w.WriteRaw (XPathNavigatorToString ((XPathNavigator) o));
280 w.WriteString (o.ToString ());
283 w.WriteEndElement ();
285 w.WriteEndElement ();
286 w.WriteEndElement ();
288 w.WriteEndElement ();
290 w.Flush (); // for XmlWriter
291 log_writer.WriteLine ();
292 log_writer.Flush (); // for TextWriter
295 static readonly XmlWriterSettings xml_writer_settings = new XmlWriterSettings () { OmitXmlDeclaration = true };
298 // I avoided OuterXml which includes indentation.
299 static string XPathNavigatorToString (XPathNavigator nav)
301 var sw = new StringWriter ();
302 using (var xw = XmlWriter.Create (sw, xml_writer_settings))
303 nav.WriteSubtree (xw);
304 return sw.ToString ();
308 static readonly string e2e_ns = "http://schemas.microsoft.com/2004/06/E2ETraceEvent";
309 static readonly string sys_ns = "http://schemas.microsoft.com/2004/06/windows/eventlog/system";