166 lines
6.3 KiB
C#
166 lines
6.3 KiB
C#
using System;
|
|
|
|
namespace Kreta.Framework.Logging
|
|
{
|
|
/// <summary>
|
|
/// Adatbázis naplóvezető osztály.
|
|
/// </summary>
|
|
public sealed class DatabaseLogWriter : LogWriterBase
|
|
{
|
|
private string m_ConnectionString;
|
|
private string m_ServerName;
|
|
|
|
private const string insertCommandText = @"
|
|
INSERT INTO T_LOG (C_TIMESTAMP, C_SERVER, C_EVENTTYPEID, C_LEVELID, C_SESSIONID, C_DETAILS, C_GENERATEDID, SERIAL)
|
|
VALUES (GETDATE(), :pSERVER, :pEVENTTYPE, :pLEVEL, :pSESSIONID, :pDETAILS, :pGENERATEDID, 0)";
|
|
|
|
private const string insertCommandTextWithIntezmeny = @"
|
|
INSERT INTO T_LOG (C_TIMESTAMP, C_SERVER, C_EVENTTYPEID, C_LEVELID, C_SESSIONID, C_DETAILS, C_GENERATEDID, C_INTEZMENYID, C_TANEVID, SERIAL)
|
|
VALUES (GETDATE(), :pSERVER, :pEVENTTYPE, :pLEVEL, :pSESSIONID, :pDETAILS, :pGENERATEDID, :pINTEZMENYID, :pTANEVID, 0)";
|
|
|
|
/// <summary>
|
|
/// Az osztály konstruktora.
|
|
/// </summary>
|
|
/// <param name="connectionString">Az adatbázis kapcsolatot leíró karakterlánc</param>
|
|
/// <param name="serverName">A naplózót használó kiszolgáló neve</param>
|
|
public DatabaseLogWriter(string connectionString, string serverName)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(connectionString))
|
|
{
|
|
throw new ArgumentNullException(nameof(connectionString));
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(serverName))
|
|
{
|
|
throw new ArgumentNullException(nameof(serverName));
|
|
}
|
|
|
|
m_ConnectionString = connectionString;
|
|
m_ServerName = serverName;
|
|
}
|
|
|
|
private string ConnectionString
|
|
{
|
|
get
|
|
{
|
|
return m_ConnectionString;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Naplóba vezeti az üzenetet.
|
|
/// </summary>
|
|
/// <param name="message">A naplóba vezetendő üzenet</param>
|
|
protected override Guid? DoLog(ILogMessage message)
|
|
{
|
|
var logId = Guid.NewGuid();
|
|
DbLog(message, logId);
|
|
return logId;
|
|
}
|
|
|
|
protected override void DoLog(ILogMessage message, Guid logId)
|
|
{
|
|
DbLog(message, logId);
|
|
}
|
|
|
|
private bool ActivateContext()
|
|
{
|
|
if (UserContext.Instance == null || !UserContext.Instance.Activated)
|
|
{
|
|
SDAServer.Instance.SessionManager.ActivateSystemSession();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private void DbLog(ILogMessage message, Guid logId)
|
|
{
|
|
if (message.Level < LogLevel.INFO)
|
|
{
|
|
// DEBUG-ot nem tolunk le adatbázisba!
|
|
return;
|
|
}
|
|
|
|
var sessionActivated = ActivateContext();
|
|
try
|
|
{
|
|
int? intezmenyId = null;
|
|
int? tanevId = null;
|
|
var commandText = insertCommandText;
|
|
if (UserContext.Instance != null && UserContext.Instance.IntezmenyId > 0)
|
|
{
|
|
commandText = insertCommandTextWithIntezmeny;
|
|
intezmenyId = UserContext.Instance.IntezmenyId;
|
|
if (UserContext.Instance.AktivTanevId > 0)
|
|
{
|
|
tanevId = UserContext.Instance.AktivTanevId;
|
|
}
|
|
}
|
|
|
|
using (SDA.DataProvider.SDAConnection connection = new SDA.DataProvider.SDAConnection(ConnectionString))
|
|
{
|
|
using (SDA.DataProvider.SDACommand command = connection.CreateCommand())
|
|
{
|
|
command.CommandText = commandText;
|
|
if (intezmenyId.HasValue)
|
|
{
|
|
command.Parameters.Add("pINTEZMENYID", intezmenyId.Value);
|
|
if (tanevId.HasValue)
|
|
{
|
|
command.Parameters.Add("pTANEVID", tanevId.Value);
|
|
}
|
|
else
|
|
{
|
|
command.Parameters.Add("pTANEVID", DBNull.Value);
|
|
}
|
|
}
|
|
|
|
command.Parameters.Add("pSERVER", SDA.DataProvider.SDADBType.String, 36).Value = m_ServerName;
|
|
command.Parameters.Add("pEVENTTYPE", SDA.DataProvider.SDADBType.Int, 9).Value = message.EventType;
|
|
command.Parameters.Add("pLEVEL", SDA.DataProvider.SDADBType.Int, 9).Value = message.Level;
|
|
command.Parameters.Add("pGENERATEDID", logId);
|
|
command.Parameters.Add("pSESSIONID", SDA.DataProvider.SDADBType.String, 36).Value = message.SessionId == null ? DBNull.Value : (object)message.SessionId;
|
|
|
|
string details = LogUtil.FormatMessageDetails(message, "\r\n");
|
|
if (string.IsNullOrWhiteSpace(details))
|
|
{
|
|
command.Parameters.Add("pDETAILS", SDA.DataProvider.SDADBType.LongString).Value = DBNull.Value;
|
|
}
|
|
else
|
|
{
|
|
command.Parameters.Add("pDETAILS", SDA.DataProvider.SDADBType.LongString).Value = details;
|
|
}
|
|
|
|
connection.Open();
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
catch (SDA.DataProvider.SDADataProviderException exception)
|
|
{
|
|
if (exception.Error == SDA.DataProvider.SDADataProviderError.CommunicationError)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (exception.Error == SDA.DataProvider.SDADataProviderError.UniqueKeyViolation)
|
|
{
|
|
// ezt a kivételt nyugodtan lenyelhetjük
|
|
return;
|
|
}
|
|
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
if (sessionActivated)
|
|
{
|
|
if (UserContext.Instance != null && UserContext.Instance.Activated)
|
|
{
|
|
SDAServer.Instance.SessionManager.DeactivateSystemSession();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|