Added LogWriter implementation to handle log events

This commit is contained in:
Tal Aloni 2017-03-10 16:27:22 +02:00
parent f9c77ff6a0
commit 536d4bd3a6
3 changed files with 116 additions and 8 deletions

111
SMBServer/LogWriter.cs Normal file
View file

@ -0,0 +1,111 @@
/* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
*
* You can redistribute this program and/or modify it under the terms of
* the GNU Lesser Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*/
using System;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
using Utilities;
namespace SMBServer
{
public class LogWriter
{
private string m_logsDirectoryPath;
private object m_syncLock = new object();
private FileStream m_logFile;
private DateTime? m_logFileDate;
public LogWriter()
{
Assembly assembly = Assembly.GetEntryAssembly();
if (assembly == null)
{
assembly = Assembly.GetExecutingAssembly();
}
string assemblyDirectory = Path.GetDirectoryName(assembly.Location);
if (!assemblyDirectory.EndsWith(@"\"))
{
assemblyDirectory += @"\";
}
m_logsDirectoryPath = assemblyDirectory + @"Logs\";
}
public LogWriter(string logsDirectoryPath)
{
m_logsDirectoryPath = logsDirectoryPath;
}
/// <exception cref="System.IO.IOException"></exception>
/// <exception cref="System.UnauthorizedAccessException"></exception>
private void OpenLogFile()
{
if (m_logFileDate.HasValue && m_logFileDate.Value != DateTime.Today && m_logFile != null)
{
m_logFile.Close();
m_logFile = null;
m_logFileDate = null;
}
if (m_logFileDate == null)
{
m_logFileDate = DateTime.Today;
string logFilePath = String.Format("{0}{1}-{2}-{3}.log", m_logsDirectoryPath, DateTime.Now.Year, DateTime.Now.Month.ToString("00"), DateTime.Now.Day.ToString("00"));
try
{
if (!Directory.Exists(m_logsDirectoryPath))
{
Directory.CreateDirectory(m_logsDirectoryPath);
}
m_logFile = new FileStream(logFilePath, FileMode.Append, FileAccess.Write, FileShare.Read, 0x1000, FileOptions.WriteThrough);
}
catch
{
}
}
}
public void CloseLogFile()
{
if (m_logFile != null)
{
lock (m_syncLock)
{
m_logFile.Close();
m_logFile = null;
}
}
}
public void WriteLine(string value, params object[] args)
{
WriteLine(String.Format(value, args));
}
public void WriteLine(string value)
{
lock (m_syncLock)
{
OpenLogFile();
if (m_logFile != null)
{
StreamWriter writer = new StreamWriter(m_logFile);
writer.WriteLine(value);
writer.Flush();
}
}
}
public void OnLogEntry(object sender, LogEntry entry)
{
if (entry.Severity != Severity.Trace)
{
string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ");
WriteLine("{0} {1} [{2}] {3}", entry.Severity.ToString().PadRight(12), timestamp, entry.Source, entry.Message);
}
}
}
}

View file

@ -39,6 +39,7 @@
<Compile Include="DirectoryFileSystem\DirectoryFileSystem.cs" />
<Compile Include="DirectoryFileSystem\StreamWatcher.cs" />
<Compile Include="DirectoryFileSystem\Win32Native.cs" />
<Compile Include="LogWriter.cs" />
<Compile Include="NetworkInterfaceHelper.cs" />
<Compile Include="ServerUI.cs">
<SubType>Form</SubType>

View file

@ -30,6 +30,7 @@ namespace SMBServer
public const string SettingsFileName = "Settings.xml";
private SMBLibrary.Server.SMBServer m_server;
private SMBLibrary.Server.NameServer m_nameServer;
private LogWriter m_logWriter;
public ServerUI()
{
@ -97,7 +98,8 @@ namespace SMBServer
GSSProvider securityProvider = new GSSProvider(authenticationMechanism);
m_server = new SMBLibrary.Server.SMBServer(shares, securityProvider);
m_server.OnLogEntry += new EventHandler<LogEntry>(Server_OnLogEntry);
m_logWriter = new LogWriter();
m_server.OnLogEntry += new EventHandler<LogEntry>(m_logWriter.OnLogEntry);
try
{
@ -210,6 +212,7 @@ namespace SMBServer
private void btnStop_Click(object sender, EventArgs e)
{
m_server.Stop();
m_logWriter.CloseLogFile();
btnStart.Enabled = true;
btnStop.Enabled = false;
comboIPAddress.Enabled = true;
@ -225,13 +228,6 @@ namespace SMBServer
}
}
private void Server_OnLogEntry(object sender, LogEntry entry)
{
string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ");
string message = String.Format("{0} {1} {2}", entry.Severity.ToString().PadRight(12), timestamp, entry.Message);
System.Diagnostics.Debug.Print(message);
}
private void chkSMB1_CheckedChanged(object sender, EventArgs e)
{
if (!chkSMB1.Checked)