Implemented VirtualPrinterDriver project

This commit is contained in:
Marco Batzinger 2020-10-19 17:44:50 +02:00
parent f29c84821b
commit 5c87967c3f
125 changed files with 8191 additions and 0 deletions

View file

@ -0,0 +1,81 @@
using System;
using JetBrains.Annotations;
namespace VirtualPrinter.Logging
{
// ReSharper disable once UnusedTypeParameter
public interface IVirtualPrinterLogger<out T> : IVirtualPrinterLogger
{ }
public interface IVirtualPrinterLogger
{
/// <summary>
/// Gets the name of the logger.
/// </summary>
string Name { get; }
/// <summary>
/// A value of true if logging is enabled for the Debug level, otherwise it returns false.
/// </summary>
bool IsDebugEnabled { get; }
/// <summary>
/// A value of true if logging is enabled for the Trace level, otherwise it returns false.
/// </summary>
bool IsTraceEnabled { get; }
/// <summary>
/// Writes the exception at the <c>Error</c> level.
/// </summary>
/// <param name="exception">An exception to be logged.</param>
void Error([CanBeNull]Exception exception);
/// <summary>
/// Writes the diagnostic message and exception at the <c>Error</c> level.
/// </summary>
/// <param name="exception">An exception to be logged.</param>
/// <param name="message">A <see langword="string" /> to be written.</param>
/// <param name="args">Arguments to format.</param>
void Error([CanBeNull]Exception exception, [CanBeNull]string message, [CanBeNull, ItemCanBeNull]params object[] args);
/// <summary>
/// Something failed; application may or may not continue
/// Writes the diagnostic message and exception at the <c>Error</c> level.
/// </summary>
/// <param name="message">A <see langword="string" /> to be written.</param>
/// <param name="args">Arguments to format.</param>
void Error([CanBeNull]string message, [CanBeNull, ItemCanBeNull]params object[] args);
/// <summary>
/// Something unexpected; application will continue
/// Writes the diagnostic message at the <c>Warn</c> level using the specified parameters.
/// </summary>
/// <param name="message">A <see langword="string" /> containing format items.</param>
/// <param name="args">Arguments to format.</param>
void Warn([CanBeNull]string message, [CanBeNull, ItemCanBeNull]params object[] args);
/// <summary>
/// Normal behavior like mail sent, user updated profile etc.
/// Writes the diagnostic message at the <c>Info</c> level using the specified parameters.
/// </summary>
/// <param name="message">A <see langword="string" /> containing format items.</param>
/// <param name="args">Arguments to format.</param>
void Info([CanBeNull]string message, [CanBeNull, ItemCanBeNull]params object[] args);
/// <summary>
/// For debugging; executed query, user authenticated, session expired
/// Writes the diagnostic message at the <c>Debug</c> level using the specified parameters.
/// </summary>
/// <param name="message">A <see langword="string" /> containing format items.</param>
/// <param name="args">Arguments to format.</param>
void Debug([CanBeNull]string message, [CanBeNull, ItemCanBeNull]params object[] args);
/// <summary>
/// Writes the diagnostic message at the <c>Trace</c> level using the specified parameters.
/// </summary>
/// <param name="message">A <see langword="string" /> containing format items.</param>
/// <param name="args">Arguments to format.</param>
void Trace([CanBeNull]string message, [CanBeNull, ItemCanBeNull]params object[] args);
}
}

View file

@ -0,0 +1,17 @@
using Autofac;
using Autofac.Extras.NLog;
namespace VirtualPrinter.Logging
{
public class LoggerModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterModule<NLogModule>();
builder.RegisterInstance(NLog.LogManager.GetCurrentClassLogger()).As<NLog.ILogger>().SingleInstance();
builder.RegisterGeneric(typeof(VirtualPrinterLogger<>))
.As(typeof(IVirtualPrinterLogger<>))
.SingleInstance();
}
}
}

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<variable name="log_title" value="${processname}"/>
<targets>
<target name="console" xsi:type="ColoredConsole" layout="${longdate} ${level} ${message} ${exception:format=ToString}"/>
<target xsi:type="EventLog"
name="eventlog"
log ="Application"
source="${var:log_title}"
layout="${message}${newline}${exception:format=ToString}">
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="console" />
<logger name="*" minlevel="Info" writeTo="eventlog" />
</rules>
</nlog>

View file

@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("VirtualPrinter.Logging")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("aa25364d-22d5-44b0-86a5-6fb14c686308")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View file

@ -0,0 +1,71 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{AA25364D-22D5-44B0-86A5-6FB14C686308}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>VirtualPrinter.Logging</RootNamespace>
<AssemblyName>VirtualPrinter.Logging</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\Files</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Transactions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="IVirtualPrinterLogger.cs" />
<Compile Include="LoggerModule.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="VirtualPrinterLogger.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="NLog.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="6.0.0" />
<PackageReference Include="Autofac.Extras.NLog" Version="4.0.0" />
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0-rc.2.20475.5" />
<PackageReference Include="NLog" Version="4.6.1" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0-rc.2.20475.5" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View file

@ -0,0 +1,103 @@
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using JetBrains.Annotations;
using NLog;
namespace VirtualPrinter.Logging
{
public class VirtualPrinterLogger<T> : VirtualPrinterLogger, IVirtualPrinterLogger<T>
{
public VirtualPrinterLogger() : base(ReadableTypeName.Generate(typeof(T)))
{
}
}
public static class ReadableTypeName
{
[NotNull]
public static string Generate([NotNull]Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
var provider = CodeDomProvider.CreateProvider("C#");
var typeReferenceExpression = new CodeTypeReferenceExpression(type);
using (var writer = new StringWriter())
{
provider.GenerateCodeFromExpression(typeReferenceExpression, writer, new CodeGeneratorOptions());
return writer.GetStringBuilder().ToString();
}
}
}
public class VirtualPrinterLogger : IVirtualPrinterLogger
{
private readonly ILogger _logger;
public VirtualPrinterLogger([NotNull]string loggerName)
{
if (string.IsNullOrWhiteSpace(loggerName))
{
throw new ArgumentNullException(nameof(loggerName));
}
_logger = LogManager.GetLogger(loggerName);
}
public string Name
{
get { return _logger.Name; }
}
public bool IsDebugEnabled
{
get { return _logger.IsDebugEnabled; }
}
public bool IsTraceEnabled
{
get { return _logger.IsTraceEnabled; }
}
public void Error(Exception exception)
{
_logger.Error(exception);
}
public void Error(Exception exception, string message, params object[] args)
{
_logger.Error(exception, message, args);
}
public void Error(string message, params object[] args)
{
_logger.Error(message, args);
}
public void Warn(string message, params object[] args)
{
_logger.Warn(message, args);
}
public void Info(string message, params object[] args)
{
_logger.Info(message, args);
}
public void Debug(string message, params object[] args)
{
_logger.Debug(message, args);
}
public void Trace(string message, params object[] args)
{
_logger.Trace(message, args);
}
}
}