|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using SmartStore.Core.Domain.Logging; |
| 6 | +using SmartStore.Core.Domain.Customers; |
| 7 | +using System.Diagnostics; |
| 8 | + |
| 9 | +namespace SmartStore.Core.Logging |
| 10 | +{ |
| 11 | + public class TraceLogger : DisposableObject, ILogger |
| 12 | + { |
| 13 | + private readonly TraceSource _traceSource; |
| 14 | + |
| 15 | + public TraceLogger() : this("SmartStore.log") |
| 16 | + { |
| 17 | + } |
| 18 | + |
| 19 | + public TraceLogger(string fileName) |
| 20 | + { |
| 21 | + Guard.ArgumentNotEmpty(() => fileName); |
| 22 | + |
| 23 | + _traceSource = new TraceSource("SmartStore"); |
| 24 | + _traceSource.Switch = new SourceSwitch("LogSwitch", "Error"); |
| 25 | + _traceSource.Listeners.Remove("Default"); |
| 26 | + |
| 27 | + var console = new ConsoleTraceListener(false); |
| 28 | + console.Filter = new EventTypeFilter(SourceLevels.All); |
| 29 | + console.Name = "console"; |
| 30 | + |
| 31 | + var textListener = new TextWriterTraceListener(fileName); |
| 32 | + textListener.Filter = new EventTypeFilter(SourceLevels.All); |
| 33 | + textListener.TraceOutputOptions = TraceOptions.DateTime; |
| 34 | + |
| 35 | + _traceSource.Listeners.Add(console); |
| 36 | + _traceSource.Listeners.Add(textListener); |
| 37 | + |
| 38 | + // Allow the trace source to send messages to |
| 39 | + // listeners for all event types. Currently only |
| 40 | + // error messages or higher go to the listeners. |
| 41 | + // Messages must get past the source switch to |
| 42 | + // get to the listeners, regardless of the settings |
| 43 | + // for the listeners. |
| 44 | + _traceSource.Switch.Level = SourceLevels.All; |
| 45 | + } |
| 46 | + |
| 47 | + public bool IsEnabled(LogLevel level) |
| 48 | + { |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + public void DeleteLog(Log log) |
| 53 | + { |
| 54 | + // not supported |
| 55 | + } |
| 56 | + |
| 57 | + public void ClearLog() |
| 58 | + { |
| 59 | + // not supported |
| 60 | + } |
| 61 | + |
| 62 | + public void ClearLog(DateTime toUtc, LogLevel logLevel) |
| 63 | + { |
| 64 | + // not supported |
| 65 | + } |
| 66 | + |
| 67 | + public IPagedList<Log> GetAllLogs(DateTime? fromUtc, DateTime? toUtc, string message, LogLevel? logLevel, int pageIndex, int pageSize, int minFrequency) |
| 68 | + { |
| 69 | + // not supported |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + public Log GetLogById(int logId) |
| 74 | + { |
| 75 | + // not supported |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + public IList<Log> GetLogByIds(int[] logIds) |
| 80 | + { |
| 81 | + // not supported |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + public Log InsertLog(LogContext context) |
| 86 | + { |
| 87 | + var type = LogLevelToEventType(context.LogLevel); |
| 88 | + _traceSource.TraceEvent(type, (int)type, "{0}: {1}".FormatCurrent(type.ToString().ToUpper(), context.ShortMessage)); |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + public Log InsertLog(LogLevel logLevel, string shortMessage, string fullMessage = "", Customer customer = null) |
| 93 | + { |
| 94 | + var context = new LogContext() |
| 95 | + { |
| 96 | + LogLevel = logLevel, |
| 97 | + ShortMessage = shortMessage, |
| 98 | + FullMessage = fullMessage, |
| 99 | + Customer = customer |
| 100 | + }; |
| 101 | + |
| 102 | + return InsertLog(context); |
| 103 | + } |
| 104 | + |
| 105 | + private TraceEventType LogLevelToEventType(LogLevel level) |
| 106 | + { |
| 107 | + switch (level) |
| 108 | + { |
| 109 | + case LogLevel.Debug: |
| 110 | + return TraceEventType.Verbose; |
| 111 | + case LogLevel.Error: |
| 112 | + return TraceEventType.Error; |
| 113 | + case LogLevel.Fatal: |
| 114 | + return TraceEventType.Critical; |
| 115 | + case LogLevel.Warning: |
| 116 | + return TraceEventType.Warning; |
| 117 | + default: |
| 118 | + return TraceEventType.Information; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + protected override void OnDispose(bool disposing) |
| 123 | + { |
| 124 | + _traceSource.Flush(); |
| 125 | + _traceSource.Close(); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments