DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
LoggerBase.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
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.Globalization;
19 
20 namespace Deveel.Data.Diagnostics {
21  public abstract class LoggerBase : ThreadedQueue<LogEntry> {
22  protected LoggerBase(LoggerSettings settings) {
23  Settings = settings;
24  }
25 
27  Dispose(false);
28  }
29 
30  public LoggerSettings Settings { get; private set; }
31 
32  protected override void Consume(LogEntry message) {
33  var logMessage = FormatMessage(message);
34  LogMessage(logMessage);
35  }
36 
37  private string FormatMessage(LogEntry entry) {
38  var format = Settings.MessageFormat;
39  foreach (var key in entry.Keys) {
40  var value = FormatValue(entry.GetValue(key));
41  var holder = String.Format("{0}", key);
42 
43  format = format.Replace(holder, value);
44  }
45 
46  return format;
47  }
48 
49  private string FormatValue(object value) {
50  if (value == null)
51  return String.Empty;
52 
53  if (value is string)
54  return (string) value;
55  if (value is DateTime ||
56  value is DateTimeOffset) {
57  if (String.IsNullOrEmpty(Settings.DateFormat))
58  return value.ToString();
59 
60  if (value is DateTimeOffset)
61  return ((DateTimeOffset) value).ToString(Settings.DateFormat);
62 
63  return ((DateTime) value).ToString(Settings.DateFormat);
64  }
65 
66  if (!(value is IConvertible))
67  return String.Empty;
68 
69  return (string) Convert.ChangeType(value, typeof (string), CultureInfo.InvariantCulture);
70  }
71 
72  protected abstract void LogMessage(string message);
73  }
74 }
string FormatValue(object value)
Definition: LoggerBase.cs:49
LoggerBase(LoggerSettings settings)
Definition: LoggerBase.cs:22
override void Consume(LogEntry message)
Definition: LoggerBase.cs:32
string FormatMessage(LogEntry entry)
Definition: LoggerBase.cs:37