Added Logging samples
This commit is contained in:
Родитель
f8dbb55771
Коммит
27c5931ccf
|
@ -0,0 +1,26 @@
|
|||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<configSections>
|
||||
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
|
||||
</configSections>
|
||||
|
||||
<log4net>
|
||||
|
||||
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
|
||||
<layout type="log4net.Layout.PatternLayout">
|
||||
<conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newlineExtra Info: %property{testProperty}%newline%exception"/>
|
||||
</layout>
|
||||
<filter type="log4net.Filter.LevelRangeFilter">
|
||||
<levelMin value="DEBUG"/>
|
||||
<levelMax value="FATAL"/>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<root>
|
||||
<level value="ALL"/>
|
||||
<appender-ref ref="ConsoleAppender"/>
|
||||
</root>
|
||||
|
||||
</log4net>
|
||||
|
||||
</configuration>
|
|
@ -0,0 +1,36 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net46</TargetFramework>
|
||||
<ApplicationIcon />
|
||||
<OutputType>Exe</OutputType>
|
||||
<StartupObject />
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<UnityAbstractions>..\..\..\..\Abstractions\src\Unity.Abstractions.csproj</UnityAbstractions>
|
||||
<UnityContainer>..\..\..\..\Container\src\Unity.Container.csproj</UnityContainer>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup Condition="Exists('$(UnityAbstractions)')">
|
||||
<ProjectReference Include="$(UnityAbstractions)" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="!Exists('$(UnityAbstractions)')">
|
||||
<PackageReference Include="Unity.Abstractions" Version="3.*" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
<ItemGroup Condition="Exists('$(UnityContainer)')">
|
||||
<ProjectReference Include="$(UnityContainer)" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="!Exists('$(UnityContainer)')">
|
||||
<PackageReference Include="Unity.Container" Version="5.*" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Unity.log4net" Version="1.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,54 @@
|
|||
using System;
|
||||
using log4net;
|
||||
using Unity;
|
||||
using Unity.log4net;
|
||||
|
||||
// Setup Log4Net as you would anywhere
|
||||
|
||||
// Here is the once-per-application setup information
|
||||
// Tell log4net to use the .config file for configuration
|
||||
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
|
||||
|
||||
namespace Log4net.Example
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var logger = LogManager.GetLogger(typeof(Program));
|
||||
|
||||
// Get the container
|
||||
var container = new UnityContainer();
|
||||
logger.Info("Created Container");
|
||||
|
||||
// Register extension
|
||||
container.AddExtension(new Log4NetExtension());
|
||||
logger.Info("Added Log4Net Extension");
|
||||
|
||||
// Register few types
|
||||
container.RegisterType<IService, Service>();
|
||||
logger.Info("Registered few types");
|
||||
|
||||
var service = container.Resolve<IService>();
|
||||
|
||||
logger.Info("Press any key to continue...");
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
|
||||
public interface IService
|
||||
{
|
||||
string Id { get; }
|
||||
}
|
||||
|
||||
public class Service : IService
|
||||
{
|
||||
public Service(ILog logger)
|
||||
{
|
||||
logger.DebugFormat("Resolved object with ID: {1} of type: \"{0}\" ", GetType().Name, Id);
|
||||
}
|
||||
|
||||
public string Id { get; } = Guid.NewGuid().ToString();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<UnityAbstractions>..\..\..\..\Abstractions\src\Unity.Abstractions.csproj</UnityAbstractions>
|
||||
<UnityContainer>..\..\..\..\Container\src\Unity.Container.csproj</UnityContainer>
|
||||
<ApplicationIcon />
|
||||
<OutputType>Exe</OutputType>
|
||||
<StartupObject />
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup Condition="Exists('$(UnityAbstractions)')">
|
||||
<ProjectReference Include="$(UnityAbstractions)" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="!Exists('$(UnityAbstractions)')">
|
||||
<PackageReference Include="Unity.Abstractions" Version="3.*" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
<ItemGroup Condition="Exists('$(UnityContainer)')">
|
||||
<ProjectReference Include="$(UnityContainer)" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="!Exists('$(UnityContainer)')">
|
||||
<PackageReference Include="Unity.Container" Version="5.*" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.1" />
|
||||
<PackageReference Include="Unity.Microsoft.Logging" Version="1.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,57 @@
|
|||
using System;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Console;
|
||||
using Unity;
|
||||
using Unity.Microsoft.Logging;
|
||||
|
||||
namespace Microsoft.Logging.Example
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// Create and configure LoggerFactory
|
||||
ILoggerFactory loggerFactory = new LoggerFactory();
|
||||
loggerFactory.AddProvider(
|
||||
new ConsoleLoggerProvider((text, logLevel) => logLevel >= LogLevel.Debug, false));
|
||||
|
||||
// Create local logger
|
||||
ILogger logger = loggerFactory.CreateLogger<Program>();
|
||||
|
||||
// Get the container
|
||||
var container = new UnityContainer();
|
||||
logger.LogInformation("Created Container");
|
||||
|
||||
// Register extension and pass it configured factory
|
||||
|
||||
container.AddExtension(new LoggingExtension(loggerFactory));
|
||||
logger.LogInformation("Added Log4Net Extension");
|
||||
|
||||
// Register few types
|
||||
container.RegisterType<IService, Service>();
|
||||
logger.LogInformation("Registered few types");
|
||||
|
||||
var service = container.Resolve<IService>();
|
||||
|
||||
logger.LogInformation("Press any key to continue...");
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface IService
|
||||
{
|
||||
string Id { get; }
|
||||
}
|
||||
|
||||
public class Service : IService
|
||||
{
|
||||
public Service(ILogger<Service> logger)
|
||||
{
|
||||
logger.LogDebug($"Resolved object with ID: {Id} of type: \"{GetType().Name}\" ");
|
||||
}
|
||||
|
||||
public string Id { get; } = Guid.NewGuid().ToString();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
<?xml version="1.0"?>
|
||||
<configuration>
|
||||
<configSections>
|
||||
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
|
||||
</configSections>
|
||||
|
||||
<nlog autoReload="true">
|
||||
|
||||
<targets async="true">
|
||||
|
||||
<target type="Console" name="n">
|
||||
<layout type="JsonLayout" SuppressSpaces="false">
|
||||
<attribute name="process_name" layout="${processname}" />
|
||||
<attribute name="short_message" layout="${message}" />
|
||||
</layout>
|
||||
</target>
|
||||
</targets>
|
||||
|
||||
<rules>
|
||||
<logger name="*" minlevel="Debug" writeTo="n"/>
|
||||
</rules>
|
||||
</nlog>
|
||||
|
||||
</configuration>
|
|
@ -0,0 +1,36 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net46</TargetFramework>
|
||||
<ApplicationIcon />
|
||||
<OutputType>Exe</OutputType>
|
||||
<StartupObject />
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<UnityAbstractions>..\..\..\..\Abstractions\src\Unity.Abstractions.csproj</UnityAbstractions>
|
||||
<UnityContainer>..\..\..\..\Container\src\Unity.Container.csproj</UnityContainer>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup Condition="Exists('$(UnityAbstractions)')">
|
||||
<ProjectReference Include="$(UnityAbstractions)" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="!Exists('$(UnityAbstractions)')">
|
||||
<PackageReference Include="Unity.Abstractions" Version="3.*" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
<ItemGroup Condition="Exists('$(UnityContainer)')">
|
||||
<ProjectReference Include="$(UnityContainer)" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="!Exists('$(UnityContainer)')">
|
||||
<PackageReference Include="Unity.Container" Version="5.*" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Unity.NLog" Version="1.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,48 @@
|
|||
using System;
|
||||
using Unity;
|
||||
using Unity.NLog;
|
||||
|
||||
namespace NLog.Example
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
// Get the container
|
||||
var container = new UnityContainer();
|
||||
logger.Info("Created Container");
|
||||
|
||||
// Register extension
|
||||
container.AddExtension(new NLogExtension());
|
||||
logger.Info("Added NLog Extension");
|
||||
|
||||
// Register few types
|
||||
container.RegisterType<IService, Service>();
|
||||
logger.Info("Registered few types");
|
||||
|
||||
var service = container.Resolve<IService>();
|
||||
|
||||
logger.Info("Press any key to continue...");
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface IService
|
||||
{
|
||||
string Id { get; }
|
||||
}
|
||||
|
||||
public class Service : IService
|
||||
{
|
||||
public Service(ILogger logger)
|
||||
{
|
||||
logger.Debug("Resolved object with ID: {1} of type: \"{0}\" ", GetType().Name, Id);
|
||||
}
|
||||
|
||||
public string Id { get; } = Guid.NewGuid().ToString();
|
||||
}
|
||||
|
||||
}
|
Загрузка…
Ссылка в новой задаче