-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/PeterKottas/DotNetCore.Wi…
- Loading branch information
Showing
10 changed files
with
275 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...tes/PeterKottas.DotNetCore.WindowsService.StandardTemplate/.template.config/template.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"author": "Peter Kottas", | ||
"classifications": [ "Microservices", "Console", "Boilerplate", "Dependency Injection", "Logging", "Configuration" ], | ||
"name": "Microservice - Standard w/ DI, Configuration and Logging Extensions", | ||
"identity": "PeterKottas.DotNetCore.WindowsService.StandardTemplate", | ||
"tags": { | ||
"language": "C#" | ||
}, | ||
"shortName": "mcrsvc-std", | ||
"guids": [ "dc46e9be-12b0-43c5-ac94-5c7019d59196" ], | ||
"sourceName": "PeterKottas.DotNetCore.WindowsService.StandardTemplate" | ||
} |
45 changes: 45 additions & 0 deletions
45
Source/Templates/PeterKottas.DotNetCore.WindowsService.StandardTemplate/ExampleService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.PlatformAbstractions; | ||
using PeterKottas.DotNetCore.WindowsService.Base; | ||
using PeterKottas.DotNetCore.WindowsService.Interfaces; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace PeterKottas.DotNetCore.WindowsService.StandardTemplate | ||
{ | ||
public class ExampleService : MicroService, IMicroService | ||
{ | ||
private IMicroServiceController _controller; | ||
private ILogger<ExampleService> _logger; | ||
|
||
public ExampleService() | ||
{ | ||
_controller = null; | ||
} | ||
|
||
public ExampleService(IMicroServiceController controller, ILogger<ExampleService> logger) | ||
{ | ||
_controller = controller; | ||
_logger = logger; | ||
} | ||
|
||
|
||
public void Start() | ||
{ | ||
StartBase(); | ||
Timers.Start("Poller", 1000, () => | ||
{ | ||
_logger.LogInformation(string.Format("Polling at {0}\n", DateTime.Now.ToString("o"))); | ||
}); | ||
_logger.LogTrace("Started\n"); | ||
} | ||
|
||
public void Stop() | ||
{ | ||
StopBase(); | ||
_logger.LogTrace("Stopped\n"); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
Source/Templates/PeterKottas.DotNetCore.WindowsService.StandardTemplate/LogFileProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace PeterKottas.DotNetCore.WindowsService.StandardTemplate | ||
{ | ||
// Thanks to Andrew Lock for basic details needed here: https://andrewlock.net/creating-a-rolling-file-logging-provider-for-asp-net-core-2-0/ | ||
public class LogFileProvider : ILoggerProvider | ||
{ | ||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
return new FileLogger(this, categoryName); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
// TODO: Handle File IO mechanics | ||
} | ||
|
||
public void AddMessage(DateTimeOffset timestamp, string message) | ||
{ | ||
// TODO: Handle File IO mechanics | ||
} | ||
} | ||
|
||
public class FileLogger : ILogger | ||
{ | ||
LogFileProvider _provider; | ||
string _category; | ||
public FileLogger(LogFileProvider provider, string categoryName) | ||
{ | ||
_provider = provider; | ||
_category = categoryName; | ||
} | ||
public IDisposable BeginScope<TState>(TState state) | ||
{ | ||
return null; | ||
} | ||
|
||
public bool IsEnabled(LogLevel logLevel) | ||
{ | ||
if (logLevel == LogLevel.None) | ||
{ | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public void Log<TState>(DateTimeOffset timestamp, LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) | ||
{ | ||
if (!IsEnabled(logLevel)) | ||
{ | ||
return; | ||
} | ||
|
||
var builder = new StringBuilder(); | ||
builder.Append(timestamp.ToString("yyyy-MM-dd HH:mm:ss.fff zzz")); | ||
builder.Append(" ["); | ||
builder.Append(logLevel.ToString()); | ||
builder.Append("] "); | ||
builder.Append(_category); | ||
builder.Append(": "); | ||
builder.AppendLine(formatter(state, exception)); | ||
|
||
if (exception != null) | ||
{ | ||
builder.AppendLine(exception.ToString()); | ||
} | ||
|
||
_provider.AddMessage(timestamp, builder.ToString()); | ||
} | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) | ||
{ | ||
Log(DateTimeOffset.Now, logLevel, eventId, state, exception, formatter); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...owsService.StandardTemplate/PeterKottas.DotNetCore.WindowsService.StandardTemplate.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" /> | ||
<PackageReference Include="PeterKottas.DotNetCore.WindowsService" Version="2.0.6" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="appsettings.Development.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
<None Update="appsettings.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
|
||
</Project> |
73 changes: 73 additions & 0 deletions
73
Source/Templates/PeterKottas.DotNetCore.WindowsService.StandardTemplate/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.PlatformAbstractions; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.FileProviders; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace PeterKottas.DotNetCore.WindowsService.StandardTemplate | ||
{ | ||
class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
#if !DEBUG | ||
var configuration = new ConfigurationBuilder() | ||
.SetBasePath(PlatformServices.Default.Application.ApplicationBasePath) | ||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) | ||
.Build(); | ||
#else | ||
var configuration = new ConfigurationBuilder() | ||
.SetBasePath(PlatformServices.Default.Application.ApplicationBasePath) | ||
.AddJsonFile("appsettings.Development.json", optional: false, reloadOnChange: true) | ||
.Build(); | ||
#endif | ||
|
||
var svcProvider = new ServiceCollection() | ||
.AddLogging(builder => | ||
{ | ||
builder | ||
.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace) | ||
.AddProvider(new LogFileProvider()); // Implemented vanilla LogFile provider but is easily swapped for Nlog or SeriLog (et al.) providers | ||
|
||
}) | ||
.AddOptions() | ||
.AddSingleton(new LoggerFactory() | ||
.AddConsole()) | ||
.BuildServiceProvider(); | ||
|
||
var _logger = svcProvider.GetRequiredService<ILoggerFactory>().CreateLogger<Program>(); | ||
|
||
|
||
ServiceRunner<ExampleService>.Run(config => | ||
{ | ||
var name = config.GetDefaultName(); | ||
config.Service(serviceConfig => | ||
{ | ||
serviceConfig.ServiceFactory((extraArguments, controller) => | ||
{ | ||
return new ExampleService(controller, svcProvider.GetRequiredService<ILoggerFactory>().CreateLogger<ExampleService>()); | ||
}); | ||
|
||
serviceConfig.OnStart((service, extraParams) => | ||
{ | ||
_logger.LogTrace("Service {0} started", name); | ||
service.Start(); | ||
}); | ||
|
||
serviceConfig.OnStop(service => | ||
{ | ||
_logger.LogTrace("Service {0} stopped", name); | ||
service.Stop(); | ||
}); | ||
|
||
serviceConfig.OnError(e => | ||
{ | ||
_logger.LogError(e, string.Format("Service {0} errored with exception", name)); | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...lates/PeterKottas.DotNetCore.WindowsService.StandardTemplate/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"ConnectionString": { | ||
}, | ||
"Logging": { | ||
"IncludeScopes": false, | ||
"LogLevel": { | ||
"Default": "Debug", | ||
"System": "Information", | ||
"Microsoft": "Information" | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Source/Templates/PeterKottas.DotNetCore.WindowsService.StandardTemplate/appsettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"ConnectionString": { | ||
|
||
}, | ||
"Logging": { | ||
"IncludeScopes": false, | ||
"Debug": { | ||
"LogLevel": { | ||
"Default": "Warning" | ||
} | ||
}, | ||
"Console": { | ||
"LogLevel": { | ||
"Default": "Warning" | ||
} | ||
} | ||
} | ||
} |