WIP
parent
472f131207
commit
df18b95751
|
@ -0,0 +1,17 @@
|
|||
using System;
|
||||
namespace sharp.logging
|
||||
{
|
||||
public class ConsoleLogger : Logger
|
||||
{
|
||||
public ConsoleLogger()
|
||||
{
|
||||
}
|
||||
|
||||
public override void WriteEntry(string[] entry)
|
||||
{
|
||||
foreach (string line in entry){
|
||||
Console.WriteLine(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,130 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace sharp.logging
|
||||
{
|
||||
public class FileLogger : Logger, IDisposable
|
||||
{
|
||||
FileStream fileStream;
|
||||
TextWriter writer;
|
||||
|
||||
bool SendToConsole;
|
||||
TextWriter stdout, stderr;
|
||||
|
||||
public FileLogger(string filename)
|
||||
{
|
||||
openLogFile(filename);
|
||||
}
|
||||
|
||||
public FileLogger(string filename,bool sendToConsole,bool captureConsole)
|
||||
{
|
||||
openLogFile(filename);
|
||||
SendToConsole = sendToConsole;
|
||||
|
||||
if (SendToConsole || captureConsole){
|
||||
stdout = Console.Out;
|
||||
}
|
||||
if (captureConsole)
|
||||
{
|
||||
stderr = Console.Error;
|
||||
|
||||
Console.SetOut( new LoggingWriter(this) );
|
||||
Console.SetError( new LoggingWriter(this,LogLevel.ERROR));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void openLogFile(string filename)
|
||||
{
|
||||
fileStream = new FileStream(filename, FileMode.Append);
|
||||
writer = new StreamWriter(fileStream);
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
if (writer != null)
|
||||
{
|
||||
writer.Close();
|
||||
fileStream.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (writer != null)
|
||||
{
|
||||
writer.Close();
|
||||
writer.Dispose();
|
||||
writer = null;
|
||||
}
|
||||
if (fileStream != null)
|
||||
{
|
||||
fileStream.Close();
|
||||
fileStream.Dispose();
|
||||
fileStream = null;
|
||||
}
|
||||
if (stdout != null){
|
||||
Console.SetOut(stdout);
|
||||
Console.SetError(stderr);
|
||||
}
|
||||
}
|
||||
|
||||
public override void WriteEntry(string[] entry)
|
||||
{
|
||||
if (writer == null)
|
||||
{
|
||||
throw new ObjectDisposedException("FileLogger has already been disposed");
|
||||
}
|
||||
foreach (String line in entry)
|
||||
{
|
||||
writer.WriteLine(line);
|
||||
writer.Flush();
|
||||
|
||||
if (stdout != null){
|
||||
stdout.WriteLine(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class LoggingWriter : TextWriter
|
||||
{
|
||||
Logger logger;
|
||||
LogLevel level;
|
||||
|
||||
List<char> linebuffer = new List<char>();
|
||||
|
||||
public LoggingWriter(Logger logger,LogLevel level = LogLevel.INFO)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public override void Write(char value)
|
||||
{
|
||||
if (value == '\n'){
|
||||
logger.Log(level,new String(linebuffer.ToArray()));
|
||||
linebuffer.Clear();
|
||||
} else {
|
||||
linebuffer.Add(value);
|
||||
}
|
||||
}
|
||||
|
||||
public override void WriteLine(string format, params object[] args)
|
||||
{
|
||||
logger.Log(level,format,args);
|
||||
}
|
||||
|
||||
public override Encoding Encoding
|
||||
{
|
||||
get
|
||||
{
|
||||
return Encoding.UTF8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
namespace sharp.logging
|
||||
{
|
||||
public enum LogLevel
|
||||
{
|
||||
FATAL = 0,
|
||||
ERROR = 5,
|
||||
WARNING = 10,
|
||||
INFO = 15,
|
||||
DEBUG = 30,
|
||||
DEBUGDETAIL = 40,
|
||||
DEBUGFULL = 80
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
namespace sharp.logging
|
||||
{
|
||||
public abstract class Logger
|
||||
{
|
||||
public static Logger DefaultLogger { get; set; } = new ConsoleLogger();
|
||||
|
||||
public Logger()
|
||||
{
|
||||
}
|
||||
|
||||
public void Log(LogLevel level,string message, params object[] args)
|
||||
{
|
||||
Log(level,String.Format(message, args));
|
||||
}
|
||||
|
||||
public void Log(string message,params object[] args){
|
||||
Log(LogLevel.INFO,String.Format(message, args));
|
||||
}
|
||||
|
||||
public void Log(string message)
|
||||
{
|
||||
Log(LogLevel.INFO, message);
|
||||
}
|
||||
public void Log(LogLevel level,string message)
|
||||
{
|
||||
string[] lines = message.Split('\n');
|
||||
|
||||
lines[0] = String.Format("{0} [{1}] [ {3,-10} ] {2}",
|
||||
DateTime.Now.ToLocalTime(),
|
||||
Process.GetCurrentProcess().Id,
|
||||
lines[0],
|
||||
level.ToString()
|
||||
);
|
||||
|
||||
for (int n = 1; n < lines.Length; n++){
|
||||
lines[n] = " " + lines[n];
|
||||
}
|
||||
WriteEntry( lines );
|
||||
}
|
||||
|
||||
public abstract void WriteEntry(string[] entry);
|
||||
|
||||
public virtual void Close()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
using System;
|
||||
namespace sharp.logging
|
||||
{
|
||||
public static class Logging
|
||||
{
|
||||
public static void Log(string message, params object[] args)
|
||||
{
|
||||
Log(LogLevel.INFO, message, args);
|
||||
}
|
||||
public static void Log(LogLevel level,string message, params object[] args)
|
||||
{
|
||||
Logger.DefaultLogger.Log(level,message, args);
|
||||
}
|
||||
|
||||
public static void Log(Exception e){
|
||||
Log(LogLevel.ERROR,"Exception: {0} / {1}\n{2}", e.GetType().Name, e.Message, e.StackTrace.ToString());
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
|
@ -7,7 +7,7 @@
|
|||
<OutputType>Library</OutputType>
|
||||
<RootNamespace>sharp.logging</RootNamespace>
|
||||
<AssemblyName>sharp.logging</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
|
@ -31,6 +31,11 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Logger.cs" />
|
||||
<Compile Include="ConsoleLogger.cs" />
|
||||
<Compile Include="Logging.cs" />
|
||||
<Compile Include="FileLogger.cs" />
|
||||
<Compile Include="LogLevel.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
Loading…
Reference in New Issue