![]() |
VOOZH | about |
dotnet add package Ecng.Server.Utils --version 1.0.177
NuGet\Install-Package Ecng.Server.Utils -Version 1.0.177
<PackageReference Include="Ecng.Server.Utils" Version="1.0.177" />
<PackageVersion Include="Ecng.Server.Utils" Version="1.0.177" />Directory.Packages.props
<PackageReference Include="Ecng.Server.Utils" />Project file
paket add Ecng.Server.Utils --version 1.0.177
#r "nuget: Ecng.Server.Utils, 1.0.177"
#:package Ecng.Server.Utils@1.0.177
#addin nuget:?package=Ecng.Server.Utils&version=1.0.177Install as a Cake Addin
#tool nuget:?package=Ecng.Server.Utils&version=1.0.177Install as a Cake Tool
Utilities for hosting and managing background services, providing service path helpers, logging integration, and configuration management.
Server.Utils is a library designed to simplify the creation and management of background services and server applications. It provides:
This library is part of the Ecng framework and targets .NET Standard 2.0, .NET 6.0, and .NET 10.0.
Static utility class providing service directory paths and logging configuration.
Access common service directories:
using Ecng.Server.Utils;
// Get the directory where the service executable is located
string serviceDirectory = ServicePath.ServiceDir;
Console.WriteLine($"Service directory: {serviceDirectory}");
// Get the data directory (ServiceDir/Data)
string dataDirectory = ServicePath.DataDir;
Console.WriteLine($"Data directory: {dataDirectory}");
Create a fully configured LogManager with file logging and service logging:
using Ecng.Server.Utils;
using Ecng.Logging;
using Microsoft.Extensions.Logging;
public class MyService
{
private readonly ILogger<MyService> _logger;
private readonly LogManager _logManager;
public MyService(ILogger<MyService> logger)
{
_logger = logger;
// Create LogManager with automatic configuration
_logManager = logger.CreateLogManager(
dataDir: ServicePath.DataDir,
defaultLevel: LogLevels.Info
);
// Use the log manager
_logManager.Sources.Add(new LogSource { Name = "MyService" });
}
public void DoWork()
{
// Log through Ecng LogManager
var source = _logManager.Sources[0];
source.AddInfoLog("Service is working...");
// Also logs to Microsoft.Extensions.Logging via ServiceLogListener
}
}
Trigger a service restart:
using Ecng.Server.Utils;
public void RestartService()
{
// This will exit with code 1, which can be configured
// in service configuration to trigger automatic restart
ServicePath.Restart();
}
Bridges Ecng logging to Microsoft.Extensions.Logging, allowing you to use both logging systems simultaneously.
using Ecng.Logging;
using Ecng.Server.Utils;
using Microsoft.Extensions.Logging;
// In your service startup
ILogger logger = loggerFactory.CreateLogger("MyService");
var logManager = new LogManager();
// Add the service log listener
logManager.Listeners.Add(new ServiceLogListener(logger));
// Now all logs written to LogManager will also appear in Microsoft.Extensions.Logging
var source = new LogSource { Name = "MySource" };
logManager.Sources.Add(source);
source.AddInfoLog("This message appears in both logging systems");
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Ecng.Logging;
using Ecng.Server.Utils;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
// Get logger from DI
var logger = app.Services.GetRequiredService<ILogger<Program>>();
// Create LogManager with ServiceLogListener integration
var logManager = logger.CreateLogManager(
ServicePath.DataDir,
LogLevels.Debug
);
// Use LogManager in your application
app.Run();
The ServiceLogListener maps Ecng log levels to Microsoft.Extensions.Logging levels:
| Ecng LogLevel | Microsoft.Extensions.Logging |
|---|---|
| Verbose | Trace |
| Debug | Debug |
| Info | Information |
| Warning | Warning |
| Error | Error |
Abstract base class for service settings with common configuration properties.
using Ecng.Server.Utils;
using Ecng.Logging;
public class MyServiceSettings : ServiceSettingsBase
{
public MyServiceSettings()
{
// Set default values
WebApiAddress = "http://localhost:5000";
LogLevel = LogLevels.Info;
}
// Add your custom settings
public string DatabaseConnectionString { get; set; }
public int MaxConcurrentRequests { get; set; } = 100;
public TimeSpan RequestTimeout { get; set; } = TimeSpan.FromSeconds(30);
}
using Ecng.Server.Utils;
using Microsoft.Extensions.Configuration;
public class MyService
{
private readonly MyServiceSettings _settings;
public MyService(IConfiguration configuration)
{
_settings = new MyServiceSettings
{
WebApiAddress = configuration["WebApi:Address"],
LogLevel = Enum.Parse<LogLevels>(configuration["Logging:Level"]),
DatabaseConnectionString = configuration["Database:ConnectionString"]
};
}
public void Start()
{
Console.WriteLine($"Starting web API at {_settings.WebApiAddress}");
Console.WriteLine($"Log level: {_settings.LogLevel}");
}
}
using Ecng.Logging;
using Ecng.Server.Utils;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
});
}
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private LogManager _logManager;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// Initialize LogManager with file logging
_logManager = _logger.CreateLogManager(
ServicePath.DataDir,
LogLevels.Info
);
var source = new LogSource { Name = "Worker" };
_logManager.Sources.Add(source);
while (!stoppingToken.IsCancellationRequested)
{
source.AddInfoLog("Worker running at: {Time}", DateTimeOffset.Now);
await Task.Delay(10000, stoppingToken);
}
}
public override void Dispose()
{
_logManager?.Dispose();
base.Dispose();
}
}
using Ecng.Logging;
using Ecng.Server.Utils;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
var builder = WebApplication.CreateBuilder(args);
// Configure settings
var settings = new MyServiceSettings
{
WebApiAddress = builder.Configuration["WebApi:Address"],
LogLevel = Enum.Parse<LogLevels>(builder.Configuration["Logging:DefaultLevel"] ?? "Info")
};
builder.Services.AddSingleton(settings);
var app = builder.Build();
// Initialize logging
var logger = app.Services.GetRequiredService<ILogger<Program>>();
var logManager = logger.CreateLogManager(ServicePath.DataDir, settings.LogLevel);
app.MapGet("/", () => "Service is running");
app.MapGet("/restart", () =>
{
Task.Run(() => ServicePath.Restart());
return "Restarting...";
});
app.Run(settings.WebApiAddress);
using Ecng.Logging;
using Ecng.Server.Utils;
using Microsoft.Extensions.Logging;
class Program
{
static void Main(string[] args)
{
// Setup Microsoft.Extensions.Logging
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole();
builder.SetMinimumLevel(LogLevel.Information);
});
var logger = loggerFactory.CreateLogger<Program>();
// Create LogManager with dual logging
var logManager = logger.CreateLogManager(
ServicePath.DataDir,
LogLevels.Debug
);
// Add a source
var source = new LogSource { Name = "MyApp" };
logManager.Sources.Add(source);
// Log messages (appears in both console and file)
source.AddInfoLog("Application started");
source.AddDebugLog("Debug information");
source.AddWarningLog("Warning message");
// Application logic
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
source.AddInfoLog("Application stopped");
logManager.Dispose();
}
}
The LogManager created by CreateLogManager automatically saves and loads settings:
ServiceDir/
Data/
logManager.json (or .xml depending on serializer)
Logs/
2025-12-20/
logs.txt
Example logManager.json:
{
"Application": {
"LogLevel": "Info"
},
"Listeners": [
{
"Type": "FileLogListener",
"Append": true,
"FileName": "logs",
"LogDirectory": "C:\\Service\\Data\\Logs",
"SeparateByDates": "SubDirectories"
}
]
}
ServicePath.Restart() exits with code 1; configure your service manager to restart on this exit codeServicePath: Thread-safe (static properties and methods)ServiceLogListener: Thread-safe for logging operationsServiceSettingsBase: Not thread-safe; properties should be set during initializationPart of the Ecng framework. See the main repository for licensing information.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 2 NuGet packages that depend on Ecng.Server.Utils:
| Package | Downloads |
|---|---|
|
StockSharp.Studio.Runner
Runner - cross platform application to run any types of strategies |
|
|
StockSharp.Hydra.Server
Hydra server |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.177 | 0 | 6/18/2026 |
| 1.0.176 | 41 | 6/17/2026 |
| 1.0.175 | 49 | 6/15/2026 |
| 1.0.174 | 53 | 6/14/2026 |
| 1.0.173 | 55 | 6/12/2026 |
| 1.0.172 | 91 | 6/9/2026 |
| 1.0.171 | 98 | 6/8/2026 |
| 1.0.170 | 103 | 6/2/2026 |
| 1.0.169 | 101 | 6/1/2026 |
| 1.0.168 | 98 | 5/31/2026 |
| 1.0.167 | 108 | 5/15/2026 |
| 1.0.166 | 91 | 5/14/2026 |
| 1.0.165 | 103 | 5/6/2026 |
| 1.0.164 | 99 | 5/3/2026 |
| 1.0.163 | 101 | 4/30/2026 |
| 1.0.162 | 115 | 4/14/2026 |
| 1.0.161 | 111 | 4/13/2026 |
| 1.0.160 | 132 | 3/17/2026 |
| 1.0.159 | 111 | 3/17/2026 |
| 1.0.158 | 122 | 3/16/2026 |