152 lines
5.2 KiB
C#
152 lines
5.2 KiB
C#
using System;
|
|
using System.Net;
|
|
|
|
namespace MailServer.Misc.SocketServer
|
|
{
|
|
public partial class SocketLogger
|
|
{
|
|
/// <summary>
|
|
/// Converts log entries to string.
|
|
/// </summary>
|
|
/// <param name="logger">Socket logger.</param>
|
|
/// <param name="firstLogPart">Specifies if first log part of multipart log.</param>
|
|
/// <param name="lastLogPart">Specifies if last log part (logging ended).</param>
|
|
/// <returns></returns>
|
|
public static string LogEntriesToString(SocketLogger logger, bool firstLogPart, bool lastLogPart)
|
|
{
|
|
string logText = "//----- Sys: 'Session:'" + logger.SessionID + " added " + DateTime.Now + "\r\n";
|
|
if (!firstLogPart)
|
|
{
|
|
logText = "//----- Sys: 'Session:'" + logger.SessionID + " partial log continues " + DateTime.Now + "\r\n";
|
|
}
|
|
|
|
foreach (SocketLogEntry entry in logger.LogEntries)
|
|
{
|
|
if (entry.Type == SocketLogEntryType.ReadFromRemoteEP)
|
|
{
|
|
logText += CreateEntry(logger, entry.Text, ">>>");
|
|
}
|
|
else if (entry.Type == SocketLogEntryType.SendToRemoteEP)
|
|
{
|
|
logText += CreateEntry(logger, entry.Text, "<<<");
|
|
}
|
|
else
|
|
{
|
|
logText += CreateEntry(logger, entry.Text, "---");
|
|
}
|
|
}
|
|
|
|
if (lastLogPart)
|
|
{
|
|
logText += "//----- Sys: 'Session:'" + logger.SessionID + " removed " + DateTime.Now + "\r\n";
|
|
}
|
|
else
|
|
{
|
|
logText += "//----- Sys: 'Session:'" + logger.SessionID + " partial log " + DateTime.Now + "\r\n";
|
|
}
|
|
|
|
return logText;
|
|
}
|
|
private static string CreateEntry(SocketLogger logger, string text, string prefix)
|
|
{
|
|
string retVal = "";
|
|
|
|
if (text.EndsWith("\r\n"))
|
|
{
|
|
text = text.Substring(0, text.Length - 2);
|
|
}
|
|
|
|
string remIP = "xxx.xxx.xxx.xxx";
|
|
try
|
|
{
|
|
if (logger.RemoteEndPoint != null)
|
|
{
|
|
remIP = ((IPEndPoint)logger.RemoteEndPoint).Address.ToString();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
|
|
string[] lines = text.Replace("\r\n", "\n").Split('\n');
|
|
foreach (string line in lines)
|
|
{
|
|
retVal += "SessionID: " + logger.SessionID + " RemIP: " + remIP + " " + prefix + " '" + line + "'\r\n";
|
|
}
|
|
|
|
return retVal;
|
|
}
|
|
/// <summary>
|
|
/// Adds data read(from remoteEndpoint) entry.
|
|
/// </summary>
|
|
/// <param name="text">Log text.</param>
|
|
/// <param name="size">Readed text size.</param>
|
|
public void AddReadEntry(string text, long size)
|
|
{
|
|
if (m_pLoaclEndPoint == null || m_pRemoteEndPoint == null)
|
|
{
|
|
m_pLoaclEndPoint = (IPEndPoint)m_pSocket.LocalEndPoint;
|
|
m_pRemoteEndPoint = (IPEndPoint)m_pSocket.RemoteEndPoint;
|
|
}
|
|
|
|
m_pEntries.Add(new SocketLogEntry(text, size, SocketLogEntryType.ReadFromRemoteEP));
|
|
|
|
OnEntryAdded();
|
|
}
|
|
/// <summary>
|
|
/// Adds data send(to remoteEndpoint) entry.
|
|
/// </summary>
|
|
/// <param name="text">Log text.</param>
|
|
/// <param name="size">Sent text size.</param>
|
|
public void AddSendEntry(string text, long size)
|
|
{
|
|
if (m_pLoaclEndPoint == null || m_pRemoteEndPoint == null)
|
|
{
|
|
m_pLoaclEndPoint = (IPEndPoint)m_pSocket.LocalEndPoint;
|
|
m_pRemoteEndPoint = (IPEndPoint)m_pSocket.RemoteEndPoint;
|
|
}
|
|
|
|
m_pEntries.Add(new SocketLogEntry(text, size, SocketLogEntryType.SendToRemoteEP));
|
|
|
|
OnEntryAdded();
|
|
}
|
|
/// <summary>
|
|
/// Adds free text entry.
|
|
/// </summary>
|
|
/// <param name="text">Log text.</param>
|
|
public void AddTextEntry(string text)
|
|
{
|
|
m_pEntries.Add(new SocketLogEntry(text, 0, SocketLogEntryType.FreeText));
|
|
|
|
OnEntryAdded();
|
|
}
|
|
/// <summary>
|
|
/// Requests to write all in memory log entries to log log file.
|
|
/// </summary>
|
|
public void Flush()
|
|
{
|
|
if (m_pLogHandler != null)
|
|
{
|
|
m_pLogHandler(this, new Log_EventArgs(this, m_FirstLogPart, true));
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// This method is called when new loge entry has added.
|
|
/// </summary>
|
|
private void OnEntryAdded()
|
|
{
|
|
// Ask to server to write partial log
|
|
if (m_pEntries.Count > 100)
|
|
{
|
|
if (m_pLogHandler != null)
|
|
{
|
|
m_pLogHandler(this, new Log_EventArgs(this, m_FirstLogPart, false));
|
|
}
|
|
|
|
m_pEntries.Clear();
|
|
m_FirstLogPart = false;
|
|
}
|
|
}
|
|
}
|
|
}
|