3 // Atsushi Enomoto <atsushi@ximian.com>
5 // Copyright (C) 2011 Novell, Inc. http://www.novell.com
6 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System.Collections.Generic;
29 using System.Configuration;
30 using System.Diagnostics;
32 using System.ServiceModel;
33 using System.ServiceModel.Channels;
34 using System.ServiceModel.Configuration;
35 using System.ServiceModel.Diagnostics;
36 using System.Threading;
38 using System.Xml.XPath;
40 namespace System.ServiceModel
42 internal enum MessageLogSourceKind
46 ServiceLevelReceiveDatagram,
47 ServiceLevelSendDatagram,
48 // more, maybe for clients?
51 internal static class Logger
54 enum TraceEventType // dummy
69 const string xmlns = "http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace";
70 static MessageLoggingSettings settings = new MessageLoggingSettings ();
72 static TextWriter log_writer;
73 static XmlWriter xml_writer;
75 static readonly TraceSource source = new TraceSource ("System.ServiceModel");
76 static readonly TraceSource message_source = new TraceSource ("System.ServiceModel.MessageLogging");
82 Environment.GetEnvironmentVariable ("MOON_WCF_TRACE") ??
83 Environment.GetEnvironmentVariable ("MONO_WCF_TRACE");
87 log_writer = Console.Out;
90 log_writer = Console.Error;
95 if (!String.IsNullOrEmpty (env))
96 log_writer = File.CreateText (env);
97 } catch (Exception ex) {
98 Console.Error.WriteLine ("WARNING: WCF trace environment variable points to non-creatable file name: " + env);
104 if (log_writer != null)
105 xml_writer = XmlWriter.Create (log_writer, new XmlWriterSettings () { OmitXmlDeclaration = true });
108 message_source.Switch.Level = SourceLevels.Information;
112 #region logger methods
114 public static void Critical (string message, params object [] args)
116 Log (TraceEventType.Critical, message, args);
119 public static void Error (string message, params object [] args)
121 Log (TraceEventType.Error, message, args);
124 public static void Warning (string message, params object [] args)
126 Log (TraceEventType.Warning, message, args);
129 public static void Info (string message, params object [] args)
131 Log (TraceEventType.Information, message, args);
134 public static void Verbose (string message, params object [] args)
136 Log (TraceEventType.Verbose, message, args);
139 // FIXME: do we need more?
141 static void Log (TraceEventType eventType, string message, params object [] args)
143 if (log_writer != null) {
147 log_writer.Write ("[{0}] ", event_id);
149 TraceCore (TraceEventType.Information, event_id,
150 false, Guid.Empty, // FIXME
152 log_writer.WriteLine (message, args);
155 source.TraceEvent (eventType, event_id, message, args);
163 #region message logging
165 static readonly XmlWriterSettings xws = new XmlWriterSettings () { OmitXmlDeclaration = true };
167 public static void LogMessage (MessageLogSourceKind sourceKind, ref Message msg, long maxMessageSize)
169 if (log_writer != null) {
170 if (maxMessageSize > int.MaxValue)
171 throw new ArgumentOutOfRangeException ("maxMessageSize");
172 var mb = msg.CreateBufferedCopy ((int) maxMessageSize);
173 msg = mb.CreateMessage ();
174 LogMessage (new MessageLogTraceRecord (sourceKind, msg.GetType (), mb));
178 public static void LogMessage (MessageLogTraceRecord log)
180 if (log_writer != null) {
181 var sw = new StringWriter ();
183 var xw = XmlWriter.Create (sw, xws);
185 var doc = new XmlDocument ();
186 var xw = doc.CreateNavigator ().AppendChild ();
188 xw.WriteStartElement ("MessageLogTraceRecord", xmlns);
189 xw.WriteStartAttribute ("Time");
190 xw.WriteValue (log.Time);
191 xw.WriteEndAttribute ();
192 xw.WriteAttributeString ("Source", log.Source.ToString ());
193 xw.WriteAttributeString ("Type", log.Type.FullName);
194 var msg = log.Message.CreateMessage ();
196 msg.WriteMessage (xw);
197 xw.WriteEndElement ();
203 log_writer.Write ("[{0}] ", event_id);
205 TraceCore (TraceEventType.Information, event_id, /*FIXME*/false, /*FIXME*/Guid.Empty, sw);
207 TraceCore (TraceEventType.Information, event_id, /*FIXME*/false, /*FIXME*/Guid.Empty, doc.CreateNavigator ());
209 message_source.TraceData (TraceEventType.Information, event_id, doc.CreateNavigator ());
218 #region XmlWriterTraceListener compatibility
219 static void TraceCore (//TraceEventCache eventCache,
220 /*string source,*/ TraceEventType eventType, int id,
221 bool hasRelatedActivity, Guid relatedActivity,
222 /*int level, bool wrapData, */params object [] data)
224 string source = "mono(dummy)";
226 bool wrapData = true;
229 w.WriteStartElement ("E2ETraceEvent", e2e_ns);
232 w.WriteStartElement ("System", sys_ns);
233 w.WriteStartElement ("EventID", sys_ns);
234 w.WriteString (XmlConvert.ToString (id));
235 w.WriteEndElement ();
236 w.WriteStartElement ("Type", sys_ns);
237 // ...what to write here?
239 w.WriteEndElement ();
240 w.WriteStartElement ("SubType", sys_ns);
241 // FIXME: it does not seem always to match eventType value ...
242 w.WriteAttributeString ("Name", eventType.ToString ());
243 // ...what to write here?
245 w.WriteEndElement ();
246 // ...what to write here?
247 w.WriteStartElement ("Level", sys_ns);
248 w.WriteString (level.ToString ());
249 w.WriteEndElement ();
250 w.WriteStartElement ("TimeCreated", sys_ns);
251 w.WriteAttributeString ("SystemTime", XmlConvert.ToString (DateTime.Now, XmlDateTimeSerializationMode.RoundtripKind));
252 w.WriteEndElement ();
253 w.WriteStartElement ("Source", sys_ns);
254 w.WriteAttributeString ("Name", source);
255 w.WriteEndElement ();
256 w.WriteStartElement ("Correlation", sys_ns);
257 w.WriteAttributeString ("ActivityID", String.Concat ("{", Guid.Empty, "}"));
258 w.WriteEndElement ();
259 w.WriteStartElement ("Execution", sys_ns);
260 w.WriteAttributeString ("ProcessName", "mono (dummy)");
261 w.WriteAttributeString ("ProcessID", "0");
262 w.WriteAttributeString ("ThreadID", Thread.CurrentThread.ManagedThreadId.ToString ());
263 w.WriteEndElement ();
264 w.WriteStartElement ("Channel", sys_ns);
265 // ...what to write here?
266 w.WriteEndElement ();
267 w.WriteStartElement ("Computer");
268 w.WriteString ("localhost(dummy)");
269 w.WriteEndElement ();
271 w.WriteEndElement ();
274 w.WriteStartElement ("ApplicationData", e2e_ns);
275 w.WriteStartElement ("TraceData", e2e_ns);
276 foreach (object o in data) {
278 w.WriteStartElement ("DataItem", e2e_ns);
279 if (o is XPathNavigator)
280 // the output ignores xmlns difference between the parent (E2ETraceEvent and the content node).
281 // To clone such behavior, I took this approach.
282 w.WriteRaw (XPathNavigatorToString ((XPathNavigator) o));
284 w.WriteString (o.ToString ());
286 w.WriteEndElement ();
288 w.WriteEndElement ();
289 w.WriteEndElement ();
291 w.WriteEndElement ();
293 w.Flush (); // for XmlWriter
294 log_writer.WriteLine ();
295 log_writer.Flush (); // for TextWriter
298 static readonly XmlWriterSettings xml_writer_settings = new XmlWriterSettings () { OmitXmlDeclaration = true };
300 // I avoided OuterXml which includes indentation.
301 static string XPathNavigatorToString (XPathNavigator nav)
303 var sw = new StringWriter ();
304 using (var xw = XmlWriter.Create (sw, xml_writer_settings))
305 nav.WriteSubtree (xw);
306 return sw.ToString ();
309 static readonly string e2e_ns = "http://schemas.microsoft.com/2004/06/E2ETraceEvent";
310 static readonly string sys_ns = "http://schemas.microsoft.com/2004/06/windows/eventlog/system";