OLD | NEW |
(Empty) | |
| 1 /* |
| 2 Copyright 2014 Google Inc |
| 3 |
| 4 Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 you may not use this file except in compliance with the License. |
| 6 You may obtain a copy of the License at |
| 7 |
| 8 http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
| 10 Unless required by applicable law or agreed to in writing, software |
| 11 distributed under the License is distributed on an "AS IS" BASIS, |
| 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 See the License for the specific language governing permissions and |
| 14 limitations under the License. |
| 15 */ |
| 16 |
| 17 using System; |
| 18 using System.Diagnostics; |
| 19 using System.Threading; |
| 20 |
| 21 namespace Google.Apis.Logging |
| 22 { |
| 23 /// <summary>A logger implementation using <see cref="System.Diagnostics.Tra
ceSource"/>.</summary> |
| 24 public class TraceSourceLogger : ILogger |
| 25 { |
| 26 private readonly TraceSource TraceSource; |
| 27 |
| 28 /// <summary>A default instance of trace source logger.</summary> |
| 29 public static ILogger Default = new TraceSourceLogger(); |
| 30 |
| 31 /// <summary>Creates a new instance of the trace source logger using "go
ogle-api-dotnet-client".</summary> |
| 32 private TraceSourceLogger() |
| 33 { |
| 34 TraceSource = new TraceSource("google-api-dotnet-client"); |
| 35 } |
| 36 |
| 37 /// <summary>Creates a new instance of trace source logger using the giv
en type.</summary> |
| 38 public TraceSourceLogger(Type t) |
| 39 { |
| 40 TraceSource = new TraceSource(t.FullName); |
| 41 } |
| 42 |
| 43 public bool IsDebugEnabled |
| 44 { |
| 45 get { return true; } |
| 46 } |
| 47 |
| 48 public ILogger ForType(Type type) |
| 49 { |
| 50 return new TraceSourceLogger(type); |
| 51 } |
| 52 |
| 53 public ILogger ForType<T>() |
| 54 { |
| 55 return new TraceSourceLogger(typeof(T)); |
| 56 } |
| 57 |
| 58 public void Info(string message, params object[] formatArgs) |
| 59 { |
| 60 TraceEvent(TraceEventType.Information, message, formatArgs); |
| 61 } |
| 62 |
| 63 public void Warning(string message, params object[] formatArgs) |
| 64 { |
| 65 TraceEvent(TraceEventType.Warning, message, formatArgs); |
| 66 } |
| 67 |
| 68 public void Debug(string message, params object[] formatArgs) |
| 69 { |
| 70 TraceEvent(TraceEventType.Verbose, message, formatArgs); |
| 71 } |
| 72 |
| 73 public void Error(Exception exception, string message, params object[] f
ormatArgs) |
| 74 { |
| 75 TraceEvent(TraceEventType.Warning, message, formatArgs); |
| 76 TraceEvent(TraceEventType.Warning, "Exception is: ", exception.Messa
ge); |
| 77 } |
| 78 |
| 79 public void Error(string message, params object[] formatArgs) |
| 80 { |
| 81 TraceEvent(TraceEventType.Warning, message, formatArgs); |
| 82 } |
| 83 |
| 84 /// <summary>Traces the event using the current thread id.</summary> |
| 85 public void TraceEvent(TraceEventType type, string msg, params object[]
args) |
| 86 { |
| 87 TraceSource.TraceEvent(type, Thread.CurrentThread.ManagedThreadId, m
sg, args); |
| 88 } |
| 89 } |
| 90 } |
| 91 |
OLD | NEW |