NTP Analyzer  0.8.2
Analyze the operation of time servers
LogExtensions.cs
Go to the documentation of this file.
1 // Copyright (c) 2013--2017 Carsten Sonne Larsen <cs@innolan.net>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 using System;
22 using System.Data;
23 using Ntp.Common.Log;
24 
25 namespace Ntp.Data.Log
26 {
27  public static class LogExtensions
28  {
29  public static void SqlExecute(this LogBase log, string sql)
30  {
31  log.WriteLine(sql.Trim(), Severity.Trace);
32  }
33 
34  public static void SqlExecute(this LogBase log, string sql, IDataParameterCollection parameters)
35  {
36  string query = $"{sql} ";
37  foreach (IDataParameter parameter in parameters)
38  {
39  query = query.Replace($"{parameter.ParameterName} ", $"{GetDbDataParameterValue(parameter)} ");
40  query = query.Replace($"{parameter.ParameterName},", $"{GetDbDataParameterValue(parameter)},");
41  query = query.Replace($"{parameter.ParameterName};", $"{GetDbDataParameterValue(parameter)};");
42  query = query.Replace($"{parameter.ParameterName})", $"{GetDbDataParameterValue(parameter)})");
43  }
44 
45  log.WriteLine(query.Trim(), Severity.Trace);
46  }
47 
48  internal static void ApplySchemaChanges(this LogBase log)
49  {
50  log.WriteLine(
51  "Applying database changes...",
52  Severity.Notice);
53  }
54 
55  internal static void AwaitingDbLink(this LogBase log, IDbConnection connection)
56  {
57  log.WriteLine(
58  "Waiting for database link.",
59  Severity.Info);
60 
61  log.WriteLine(
62  $"Database connection timeout is {connection.ConnectionTimeout} seconds.",
63  Severity.Debug);
64 
65  log.WriteLine(
66  "DATABASE CONNECTION STRING COULD CONTAIN SENSITIVE INFORMATION.",
67  Severity.Trace);
68 
69  log.WriteLine(
70  $"DATABASE CONNECTION STRING IS [{connection.ConnectionString}]",
71  Severity.Trace);
72  }
73 
74  internal static void DbLinkDown(this LogBase log)
75  {
76  log.WriteLine("Database link is down.", Severity.Notice);
77  }
78 
79  internal static void DbLinkError(this LogBase log, Exception e)
80  {
81  log.WriteLine(e.Message, Severity.Info);
82  }
83 
84  internal static void DbLinkUp(this LogBase log)
85  {
86  log.WriteLine("Database link is up.", Severity.Notice);
87  }
88 
89  internal static void SchemaChangesApplied(this LogBase log, string version)
90  {
91  log.WriteLine($"Changes for version {version} applied.", Severity.Notice);
92  }
93 
94  internal static void SchemaUpdated(this LogBase log, string version)
95  {
96  log.WriteLine($"Database schema updated to version {version}", Severity.Notice);
97  }
98 
99  internal static void SchemaUpdateError(this LogBase log, Exception e)
100  {
101  log.WriteLine("Failed to apply database change script.", Severity.Error);
102  log.WriteLine(e);
103  }
104 
105  internal static void SchemaUpToDate(this LogBase log)
106  {
107  log.WriteLine("Database schema is latest version.", Severity.Info);
108  }
109 
110  internal static void SchemaVersionError(this LogBase log, int database, int application)
111  {
112  if (database == -1)
113  {
114  log.WriteLine("Database version is -1. Did you set 'Create Yes' in configuration ?", Severity.Notice);
115  }
116 
117  log.WriteLine(
118  $"Application version is {application} but database version is {database}.",
119  Severity.Error);
120 
121  if (database > application)
122  {
123  log.WriteLine("Update NTP Analyzer to resolve this problem.", Severity.Info);
124  }
125  else if (database < application)
126  {
127  log.WriteLine("Upgrade your database to resolve this problem.", Severity.Info);
128  }
129  }
130 
131  internal static void VersionTableCreated(this LogBase log)
132  {
133  log.WriteLine("Created new version table.", Severity.Notice);
134  }
135 
136  internal static void VersionTableCreateError(this LogBase log, Exception e)
137  {
138  log.WriteLine("Failed to create version table.", Severity.Error);
139  log.WriteLine(e);
140  }
141 
142  internal static void VersionTableFetchError(this LogBase log, Exception e)
143  {
144  log.WriteLine("Failed to fetch version number from database.", Severity.Error);
145  log.WriteLine(e);
146  }
147 
148  internal static void VersionTableInsertError(this LogBase log, Exception e)
149  {
150  log.WriteLine("Failed to insert into version table.", Severity.Error);
151  log.WriteLine(e);
152  }
153 
154  internal static void VersionTableParamError(this LogBase log)
155  {
156  log.WriteLine("Internal error: Version cannot be null.", Severity.Error);
157  }
158 
159  internal static void VersionTableUpdateError(this LogBase log, Exception e)
160  {
161  log.WriteLine("Failed to update version table.", Severity.Error);
162  log.WriteLine(e);
163  }
164 
165  private static string GetDbDataParameterValue(IDataParameter p)
166  {
167  switch (p.DbType)
168  {
169  case DbType.AnsiString:
170  case DbType.AnsiStringFixedLength:
171  case DbType.Guid:
172  case DbType.String:
173  case DbType.StringFixedLength:
174  case DbType.Xml:
175  return $"\'{p.Value.ToString().Replace("'", "''")}\'";
176  case DbType.Time:
177  return p.Value.ToString();
178  case DbType.Date:
179  case DbType.DateTime:
180  return $"\'{Convert.ToDateTime(p.Value).ToString("yyyy-MM-dd HH:mm:ss")}\'";
181  case DbType.DateTime2:
182  case DbType.DateTimeOffset:
183  return p.Value.ToString();
184  case DbType.Object:
185  return p.Value.ToString();
186  case DbType.Binary:
187  return "{binary}";
188  case DbType.Boolean:
189  return Convert.ToBoolean(p.Value) ? "1" : "0";
190  case DbType.Byte:
191  case DbType.SByte:
192  case DbType.Int16:
193  case DbType.Int32:
194  case DbType.Int64:
195  case DbType.UInt16:
196  case DbType.UInt32:
197  case DbType.UInt64:
198  return p.Value.ToString();
199  case DbType.Single:
200  case DbType.Double:
201  case DbType.Decimal:
202  case DbType.Currency:
203  case DbType.VarNumeric:
204  return p.Value.ToString();
205  default:
206  return p.Value.ToString();
207  }
208  }
209  }
210 }
static void SchemaUpdated(this LogBase log, string version)
static void SchemaChangesApplied(this LogBase log, string version)
abstract void WriteLine(string text, Severity severity)
static void SchemaUpToDate(this LogBase log)
static void VersionTableParamError(this LogBase log)
static void AwaitingDbLink(this LogBase log, IDbConnection connection)
static void SchemaVersionError(this LogBase log, int database, int application)
static void VersionTableCreateError(this LogBase log, Exception e)
static void DbLinkUp(this LogBase log)
static void SchemaUpdateError(this LogBase log, Exception e)
static string GetDbDataParameterValue(IDataParameter p)
static void VersionTableUpdateError(this LogBase log, Exception e)
var e
Definition: bootstrap.min.js:6
static void ApplySchemaChanges(this LogBase log)
static void SqlExecute(this LogBase log, string sql)
static void VersionTableInsertError(this LogBase log, Exception e)
static void VersionTableFetchError(this LogBase log, Exception e)
static void DbLinkError(this LogBase log, Exception e)
static void SqlExecute(this LogBase log, string sql, IDataParameterCollection parameters)
static void VersionTableCreated(this LogBase log)
var version
Definition: bootstrap.js:13
static void DbLinkDown(this LogBase log)