eDosStation/eDosStation/SqlLog.cs
Luc Vandenbroucke 81ab4d634c Nieuwe database
2020-02-18 14:06:52 +01:00

71 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace eDosStation
{
public class SqlLog: IDisposable
{
private bool disposed = false;
System.IO.StreamWriter wr;
public SqlLog()
{
try
{
wr = new System.IO.StreamWriter(@"c:\temp\sql.txt", true);
} catch(System.Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
if (wr?.BaseStream != null) wr.Close();
wr.Dispose();
}
disposed = true;
}
}
~SqlLog()
{
Dispose(false);
}
public void LogCommand(string ermsg)
{
if (wr.BaseStream.CanWrite)
{
wr.WriteLine($"-- {System.DateTime.Now.ToString("yyyy-MM-dd HH:mm")} --- err {ermsg} ---");
wr.Flush();
}
}
public void LogCommand(System.Data.SqlClient.SqlCommand co )
{
wr.WriteLine($"-- {System.DateTime.Now.ToString("yyyy-MM-dd HH:mm")} ---");
if (co.CommandType == System.Data.CommandType.StoredProcedure) wr.Write("exec ");
wr.Write(co.CommandText);
int paidx = 0;
foreach (System.Data.SqlClient.SqlParameter pa in co.Parameters)
{
if (paidx++ > 0) wr.Write(",");
wr.Write($" @{pa.ParameterName}={pa.Value}");
}
wr.WriteLine();
wr.WriteLine("----");
wr.Flush();
}
}
}