238 lines
7.5 KiB
C#
238 lines
7.5 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Globalization;
|
|
using System.Text;
|
|
|
|
namespace Kreta.Framework.Logging
|
|
{
|
|
/// <summary>
|
|
/// A napló alrendszer konfigjának a felülete.
|
|
/// </summary>
|
|
public interface ILogConfig
|
|
{
|
|
/// <summary>
|
|
/// A naplózási szint.
|
|
/// </summary>
|
|
LogLevel LogLevel { get; }
|
|
|
|
/// <summary>
|
|
/// Konzolra való naplózás engedélyezése.
|
|
/// </summary>
|
|
bool LogToConsole { get; }
|
|
|
|
/// <summary>
|
|
/// A naplózó kiszolgáló neve.
|
|
/// </summary>
|
|
string ServerName { get; }
|
|
|
|
/// <summary>
|
|
/// Az adatbázisba naplózás adatbázis kapcsolata.
|
|
/// </summary>
|
|
string ConnectionString { get; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// A napló alrendszer segédosztálya.
|
|
/// </summary>
|
|
public static class LogUtil
|
|
{
|
|
/// <summary>
|
|
/// Konfigurálja a napló alrendszert
|
|
/// </summary>
|
|
/// <param name="config">A napló alrendszer konfigja</param>
|
|
/// <returns>A gyökér naplvezető objektum</returns>
|
|
public static ILogWriter ConfigureLogWriters(ILogConfig config)
|
|
{
|
|
// <<Assembler>>
|
|
MulticastLogWriter result = new MulticastLogWriter(config.LogLevel);
|
|
if (config.LogToConsole)
|
|
{
|
|
result.Add(new ConsoleLogWriter());
|
|
}
|
|
result.Add(new DatabaseLogWriter(config.ConnectionString, config.ServerName));
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Megformáz egy napló üzenetet.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Elválasztónak újsorjelet használ.
|
|
/// </remarks>
|
|
/// <param name="message">A megformázandó naplóüzenet</param>
|
|
/// <returns>A formázott üzenet</returns>
|
|
public static string FormatMessage(ILogMessage message)
|
|
{
|
|
return FormatMessage(message, Environment.NewLine);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Megformáz egy napló üzenetet.
|
|
/// </summary>
|
|
/// <param name="message">A megformázandó naplóüzenet</param>
|
|
/// <param name="separator">A bejegyzés mezőinek elválasztója</param>
|
|
/// <returns>A formázott üzenet</returns>
|
|
public static string FormatMessage(ILogMessage message, string separator)
|
|
{
|
|
if (message == null)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
builder.Append("TimeStamp: ");
|
|
builder.Append(DateTime.Now.ToString(CultureInfo.GetCultureInfo(1038)));
|
|
|
|
builder.Append(separator);
|
|
builder.Append("Level: ");
|
|
builder.Append(message.Level);
|
|
|
|
builder.Append(separator);
|
|
builder.Append("Id: ");
|
|
builder.Append(message.LogEntryId);
|
|
|
|
builder.Append(separator);
|
|
builder.Append("Event: ");
|
|
builder.Append(message.EventType);
|
|
|
|
if (!string.IsNullOrWhiteSpace(message.SessionId))
|
|
{
|
|
builder.Append(separator);
|
|
builder.Append("SessionId: ");
|
|
builder.Append(message.SessionId);
|
|
}
|
|
|
|
if (message.Parameters.Count > 0)
|
|
{
|
|
foreach (DictionaryEntry entry in message.Parameters)
|
|
{
|
|
builder.Append(separator);
|
|
builder.Append(entry.Key);
|
|
builder.Append(": ");
|
|
if (entry.Value != null)
|
|
{
|
|
builder.Append(entry.Value);
|
|
}
|
|
}
|
|
}
|
|
|
|
return builder.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tömören megformáz egy napló üzenetet.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Elválasztónak '|' jelet használ.
|
|
/// </remarks>
|
|
/// <param name="message">A megformázandó naplóüzenet</param>
|
|
/// <returns>A formázott üzenet</returns>
|
|
public static string FormatMessageCompact(ILogMessage message)
|
|
{
|
|
return FormatMessageCompact(message, " | ");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tömören megformáz egy napló üzenetet.
|
|
/// </summary>
|
|
/// <param name="message">A megformázandó naplóüzenet</param>
|
|
/// <param name="separator">A bejegyzés mezőinek elválasztója</param>
|
|
/// <returns>A formázott üzenet</returns>
|
|
public static string FormatMessageCompact(ILogMessage message, string separator)
|
|
{
|
|
if (message == null)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
builder.Append(DateTime.Now.ToString(CultureInfo.GetCultureInfo(1038)));
|
|
|
|
builder.Append(separator);
|
|
builder.Append(message.Level.ToString());
|
|
|
|
builder.Append(separator);
|
|
builder.Append(message.LogEntryId);
|
|
|
|
builder.Append(separator);
|
|
builder.Append(message.EventType.ToString());
|
|
|
|
if (!string.IsNullOrWhiteSpace(message.SessionId))
|
|
{
|
|
builder.Append(separator);
|
|
builder.Append("SessionId: ");
|
|
builder.Append(message.SessionId);
|
|
}
|
|
|
|
if (message.Parameters.Count > 0)
|
|
{
|
|
foreach (DictionaryEntry entry in message.Parameters)
|
|
{
|
|
builder.AppendLine();
|
|
builder.Append(entry.Key);
|
|
builder.AppendLine(": ");
|
|
if (entry.Value != null)
|
|
{
|
|
builder.Append(entry.Value);
|
|
}
|
|
}
|
|
}
|
|
|
|
return builder.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Megformázza a megadott napló üzenet paramétereit.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Elválasztónak újsorjelet használ.
|
|
/// </remarks>
|
|
/// <param name="message">A megformázandó naplóüzenet</param>
|
|
/// <returns>A formázott üzenet</returns>
|
|
public static string FormatMessageDetails(ILogMessage message)
|
|
{
|
|
return FormatMessageDetails(message, Environment.NewLine);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Megformázza a megadott napló üzenet paramétereit.
|
|
/// </summary>
|
|
/// <param name="message">A megformázandó naplóüzenet</param>
|
|
/// <param name="separator">A bejegyzés mezőinek elválasztója</param>
|
|
/// <returns>A formázott üzenet</returns>
|
|
public static string FormatMessageDetails(ILogMessage message, string separator)
|
|
{
|
|
if (message == null)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
int count = message.Parameters.Count;
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
if (count > 0)
|
|
{
|
|
builder.Append(separator);
|
|
}
|
|
|
|
foreach (DictionaryEntry entry in message.Parameters)
|
|
{
|
|
builder.Append(entry.Key);
|
|
builder.Append(": ");
|
|
if (entry.Value != null)
|
|
{
|
|
builder.Append(entry.Value);
|
|
}
|
|
|
|
count--;
|
|
if (count > 0)
|
|
{
|
|
builder.Append(separator);
|
|
}
|
|
}
|
|
return builder.ToString();
|
|
}
|
|
}
|
|
}
|