Upgrades
parent
df18b95751
commit
ea1dcffc5a
|
@ -0,0 +1,41 @@
|
||||||
|
# Autosave files
|
||||||
|
*~
|
||||||
|
|
||||||
|
# build
|
||||||
|
[Oo]bj/
|
||||||
|
[Bb]in/
|
||||||
|
packages/
|
||||||
|
TestResults/
|
||||||
|
|
||||||
|
# globs
|
||||||
|
Makefile.in
|
||||||
|
*.DS_Store
|
||||||
|
*.sln.cache
|
||||||
|
*.suo
|
||||||
|
*.cache
|
||||||
|
*.pidb
|
||||||
|
*.userprefs
|
||||||
|
*.usertasks
|
||||||
|
config.log
|
||||||
|
config.make
|
||||||
|
config.status
|
||||||
|
aclocal.m4
|
||||||
|
install-sh
|
||||||
|
autom4te.cache/
|
||||||
|
*.user
|
||||||
|
*.tar.gz
|
||||||
|
tarballs/
|
||||||
|
test-results/
|
||||||
|
Thumbs.db
|
||||||
|
.vs/
|
||||||
|
|
||||||
|
# Mac bundle stuff
|
||||||
|
*.dmg
|
||||||
|
*.app
|
||||||
|
|
||||||
|
# resharper
|
||||||
|
*_Resharper.*
|
||||||
|
*.Resharper
|
||||||
|
|
||||||
|
# dotCover
|
||||||
|
*.dotCover
|
|
@ -1,16 +1,16 @@
|
||||||
using System;
|
using System;
|
||||||
namespace sharp.logging
|
namespace sharp.logging
|
||||||
{
|
{
|
||||||
public class ConsoleLogger : Logger
|
public class ConsoleLogger : LoggingBackend
|
||||||
{
|
{
|
||||||
public ConsoleLogger()
|
public ConsoleLogger()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void WriteEntry(string[] entry)
|
protected override void MessageReceived(LogLevel logLevel, string[] lines)
|
||||||
{
|
{
|
||||||
foreach (string line in entry){
|
foreach (string line in lines){
|
||||||
Console.WriteLine(line);
|
Logger.SysOut.WriteLine(line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
166
FileLogger.cs
166
FileLogger.cs
|
@ -1,130 +1,62 @@
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace sharp.logging
|
namespace sharp.logging
|
||||||
{
|
{
|
||||||
public class FileLogger : Logger, IDisposable
|
public class FileLogger : LoggingBackend, IDisposable
|
||||||
{
|
{
|
||||||
FileStream fileStream;
|
FileStream fileStream;
|
||||||
TextWriter writer;
|
TextWriter writer;
|
||||||
|
|
||||||
bool SendToConsole;
|
public FileLogger(string filename)
|
||||||
TextWriter stdout, stderr;
|
{
|
||||||
|
openLogFile(filename);
|
||||||
|
}
|
||||||
|
|
||||||
public FileLogger(string filename)
|
private void openLogFile(string filename)
|
||||||
{
|
{
|
||||||
openLogFile(filename);
|
fileStream = new FileStream(filename, FileMode.Append);
|
||||||
}
|
writer = new StreamWriter(fileStream);
|
||||||
|
}
|
||||||
|
|
||||||
public FileLogger(string filename,bool sendToConsole,bool captureConsole)
|
public void Close()
|
||||||
{
|
{
|
||||||
openLogFile(filename);
|
if (writer != null)
|
||||||
SendToConsole = sendToConsole;
|
{
|
||||||
|
writer.Close();
|
||||||
|
fileStream.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (SendToConsole || captureConsole){
|
public void Dispose()
|
||||||
stdout = Console.Out;
|
{
|
||||||
}
|
if (writer != null)
|
||||||
if (captureConsole)
|
{
|
||||||
{
|
writer.Close();
|
||||||
stderr = Console.Error;
|
writer.Dispose();
|
||||||
|
writer = null;
|
||||||
|
}
|
||||||
|
if (fileStream != null)
|
||||||
|
{
|
||||||
|
fileStream.Close();
|
||||||
|
fileStream.Dispose();
|
||||||
|
fileStream = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Console.SetOut( new LoggingWriter(this) );
|
protected override void MessageReceived(LogLevel logLevel, string[] lines)
|
||||||
Console.SetError( new LoggingWriter(this,LogLevel.ERROR));
|
{
|
||||||
}
|
if (writer == null)
|
||||||
|
{
|
||||||
}
|
throw new ObjectDisposedException("FileLogger has already been disposed");
|
||||||
|
}
|
||||||
private void openLogFile(string filename)
|
foreach (String line in lines)
|
||||||
{
|
{
|
||||||
fileStream = new FileStream(filename, FileMode.Append);
|
writer.WriteLine(line);
|
||||||
writer = new StreamWriter(fileStream);
|
writer.Flush();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
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,12 @@
|
||||||
|
using System;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
|
namespace sharp.logging
|
||||||
|
{
|
||||||
|
public interface ILoggingBackend
|
||||||
|
{
|
||||||
|
LogLevel MinLogLevel { get; set; }
|
||||||
|
LogLevel MaxLogLevel { get; set; }
|
||||||
|
|
||||||
|
void Message(LogLevel logLevel,String[] lines);
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,6 +9,7 @@ namespace sharp.logging
|
||||||
INFO = 15,
|
INFO = 15,
|
||||||
DEBUG = 30,
|
DEBUG = 30,
|
||||||
DEBUGDETAIL = 40,
|
DEBUGDETAIL = 40,
|
||||||
DEBUGFULL = 80
|
DEBUGFULL = 80,
|
||||||
|
MAX = 128
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
75
Logger.cs
75
Logger.cs
|
@ -1,13 +1,35 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
using System.Reflection;
|
||||||
namespace sharp.logging
|
namespace sharp.logging
|
||||||
{
|
{
|
||||||
public abstract class Logger
|
public class Logger
|
||||||
{
|
{
|
||||||
public static Logger DefaultLogger { get; set; } = new ConsoleLogger();
|
public static ConsoleLogger ConsoleLogger { get; } = new ConsoleLogger();
|
||||||
|
public static Logger Default { get; set; } = new Logger(ConsoleLogger);
|
||||||
|
|
||||||
public Logger()
|
public static TextWriter SysOut = Console.Out;
|
||||||
|
public static TextWriter SysError = Console.Error;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static void CaptureConsole(Logger logger)
|
||||||
|
{
|
||||||
|
LoggingWriter outWriter = new LoggingWriter(logger);
|
||||||
|
Console.SetOut(outWriter);
|
||||||
|
LoggingWriter errorWriter = new LoggingWriter(logger, LogLevel.ERROR);
|
||||||
|
Console.SetError(errorWriter);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public HashSet<ILoggingBackend> Backends { get; } = new HashSet<ILoggingBackend>();
|
||||||
|
|
||||||
|
public Logger(ILoggingBackend backend)
|
||||||
{
|
{
|
||||||
|
Backends.Add(backend);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Log(LogLevel level,string message, params object[] args)
|
public void Log(LogLevel level,string message, params object[] args)
|
||||||
|
@ -37,14 +59,51 @@ namespace sharp.logging
|
||||||
for (int n = 1; n < lines.Length; n++){
|
for (int n = 1; n < lines.Length; n++){
|
||||||
lines[n] = " " + lines[n];
|
lines[n] = " " + lines[n];
|
||||||
}
|
}
|
||||||
WriteEntry( lines );
|
|
||||||
|
Send(level,lines);
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void WriteEntry(string[] entry);
|
private void Send(LogLevel logLevel,String[] lines)
|
||||||
|
{
|
||||||
|
foreach (ILoggingBackend backend in Backends)
|
||||||
|
backend.Message(logLevel,lines);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void Log(Exception exception)
|
||||||
|
{
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
stringBuilder.AppendFormat("Exception {0} [{1}]\n", exception.GetType().Name,exception.Message);
|
||||||
|
|
||||||
|
StackTrace stackTrace = new StackTrace(exception, true);
|
||||||
|
|
||||||
|
foreach (StackFrame stackFrame in stackTrace.GetFrames())
|
||||||
|
{
|
||||||
|
MethodBase methodBase = stackFrame.GetMethod();
|
||||||
|
MethodBody methodBody = methodBase.GetMethodBody();
|
||||||
|
|
||||||
|
stringBuilder.AppendFormat(" {0} in {1}:{2}\n",
|
||||||
|
methodBase.Name,
|
||||||
|
stackFrame.GetFileName(),
|
||||||
|
stackFrame.GetFileLineNumber()
|
||||||
|
);
|
||||||
|
//if (methodBody != null)
|
||||||
|
//{
|
||||||
|
// foreach (LocalVariableInfo lvi in methodBody.LocalVariables)
|
||||||
|
// {
|
||||||
|
// stringBuilder.AppendFormat(" {0}\n", lvi);
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
|
||||||
|
Log(LogLevel.ERROR, stringBuilder.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public virtual void Close()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ namespace sharp.logging
|
||||||
}
|
}
|
||||||
public static void Log(LogLevel level,string message, params object[] args)
|
public static void Log(LogLevel level,string message, params object[] args)
|
||||||
{
|
{
|
||||||
Logger.DefaultLogger.Log(level,message, args);
|
Logger.Default.Log(level,message, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Log(Exception e){
|
public static void Log(Exception e){
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
using System;
|
||||||
|
namespace sharp.logging
|
||||||
|
{
|
||||||
|
public abstract class LoggingBackend : ILoggingBackend
|
||||||
|
{
|
||||||
|
public LogLevel MinLogLevel { get; set; } = LogLevel.FATAL;
|
||||||
|
public LogLevel MaxLogLevel { get; set; } = LogLevel.MAX;
|
||||||
|
|
||||||
|
public LoggingBackend()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Message(LogLevel logLevel, string[] lines)
|
||||||
|
{
|
||||||
|
if ((logLevel >= MinLogLevel) && (logLevel <= MaxLogLevel))
|
||||||
|
MessageReceived(logLevel, lines);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void MessageReceived(LogLevel logLevel, String[] lines);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace sharp.logging
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
|
@ -7,7 +7,7 @@
|
||||||
<OutputType>Library</OutputType>
|
<OutputType>Library</OutputType>
|
||||||
<RootNamespace>sharp.logging</RootNamespace>
|
<RootNamespace>sharp.logging</RootNamespace>
|
||||||
<AssemblyName>sharp.logging</AssemblyName>
|
<AssemblyName>sharp.logging</AssemblyName>
|
||||||
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
|
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
<DebugSymbols>true</DebugSymbols>
|
<DebugSymbols>true</DebugSymbols>
|
||||||
|
@ -36,6 +36,9 @@
|
||||||
<Compile Include="Logging.cs" />
|
<Compile Include="Logging.cs" />
|
||||||
<Compile Include="FileLogger.cs" />
|
<Compile Include="FileLogger.cs" />
|
||||||
<Compile Include="LogLevel.cs" />
|
<Compile Include="LogLevel.cs" />
|
||||||
|
<Compile Include="ILoggingBackend.cs" />
|
||||||
|
<Compile Include="LoggingWriter.cs" />
|
||||||
|
<Compile Include="LoggingBackend.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||||
</Project>
|
</Project>
|
Loading…
Reference in New Issue