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     {
SqlExecute(this LogBase log, string sql)29         public static void SqlExecute(this LogBase log, string sql)
30         {
31             log.WriteLine(sql.Trim(), Severity.Trace);
32         }
33 
SqlExecute(this LogBase log, string sql, IDataParameterCollection parameters)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 
ApplySchemaChanges(this LogBase log)48         internal static void ApplySchemaChanges(this LogBase log)
49         {
50             log.WriteLine(
51                 "Applying database changes...",
52                 Severity.Notice);
53         }
54 
AwaitingDbLink(this LogBase log, IDbConnection connection)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 
DbLinkDown(this LogBase log)74         internal static void DbLinkDown(this LogBase log)
75         {
76             log.WriteLine("Database link is down.", Severity.Notice);
77         }
78 
DbLinkError(this LogBase log, Exception e)79         internal static void DbLinkError(this LogBase log, Exception e)
80         {
81             log.WriteLine(e.Message, Severity.Info);
82         }
83 
DbLinkUp(this LogBase log)84         internal static void DbLinkUp(this LogBase log)
85         {
86             log.WriteLine("Database link is up.", Severity.Notice);
87         }
88 
SchemaChangesApplied(this LogBase log, string version)89         internal static void SchemaChangesApplied(this LogBase log, string version)
90         {
91             log.WriteLine($"Changes for version {version} applied.", Severity.Notice);
92         }
93 
SchemaUpdated(this LogBase log, string version)94         internal static void SchemaUpdated(this LogBase log, string version)
95         {
96             log.WriteLine($"Database schema updated to version {version}", Severity.Notice);
97         }
98 
SchemaUpdateError(this LogBase log, Exception e)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 
SchemaUpToDate(this LogBase log)105         internal static void SchemaUpToDate(this LogBase log)
106         {
107             log.WriteLine("Database schema is latest version.", Severity.Info);
108         }
109 
SchemaVersionError(this LogBase log, int database, int application)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 
VersionTableCreated(this LogBase log)131         internal static void VersionTableCreated(this LogBase log)
132         {
133             log.WriteLine("Created new version table.", Severity.Notice);
134         }
135 
VersionTableCreateError(this LogBase log, Exception e)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 
VersionTableFetchError(this LogBase log, Exception e)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 
VersionTableInsertError(this LogBase log, Exception e)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 
VersionTableParamError(this LogBase log)154         internal static void VersionTableParamError(this LogBase log)
155         {
156             log.WriteLine("Internal error: Version cannot be null.", Severity.Error);
157         }
158 
VersionTableUpdateError(this LogBase log, Exception e)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 
GetDbDataParameterValue(IDataParameter p)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 }