VOOZH about

URL: https://www.nuget.org/packages/Ecng.Server.Utils/

⇱ NuGet Gallery | Ecng.Server.Utils 1.0.177




👁 Image
Ecng.Server.Utils 1.0.177

dotnet add package Ecng.Server.Utils --version 1.0.177
 
 
NuGet\Install-Package Ecng.Server.Utils -Version 1.0.177
 
 
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ecng.Server.Utils" Version="1.0.177" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ecng.Server.Utils" Version="1.0.177" />
 
Directory.Packages.props
<PackageReference Include="Ecng.Server.Utils" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Ecng.Server.Utils --version 1.0.177
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Ecng.Server.Utils, 1.0.177"
 
 
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Ecng.Server.Utils@1.0.177
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Ecng.Server.Utils&version=1.0.177
 
Install as a Cake Addin
#tool nuget:?package=Ecng.Server.Utils&version=1.0.177
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Server.Utils

Utilities for hosting and managing background services, providing service path helpers, logging integration, and configuration management.

Overview

Server.Utils is a library designed to simplify the creation and management of background services and server applications. It provides:

  • Service path and directory utilities
  • Integration with Microsoft.Extensions.Logging
  • Log management and configuration
  • Base settings classes for server applications

Installation

This library is part of the Ecng framework and targets .NET Standard 2.0, .NET 6.0, and .NET 10.0.

Components

ServicePath

Static utility class providing service directory paths and logging configuration.

Service Directory Properties

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 and Configure LogManager

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
 }
}
Service Restart

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();
}

ServiceLogListener

Bridges Ecng logging to Microsoft.Extensions.Logging, allowing you to use both logging systems simultaneously.

Basic Usage
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");
Integration with ASP.NET Core
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();
Log Level Mapping

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

ServiceSettingsBase

Abstract base class for service settings with common configuration properties.

Create Custom Settings
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);
}
Use Settings in Service
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}");
 }
}

Complete Usage Examples

Windows Service with Logging

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();
 }
}

ASP.NET Core Service with Custom Settings

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);

Standalone Console Application

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();
 }
}

Configuration Files

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"
 }
 ]
}

Requirements

  • .NET Standard 2.0, .NET 6.0, or .NET 10.0
  • Dependencies:
    • Ecng.Logging
    • Ecng.Serialization
    • Microsoft.Extensions.Logging (for ServiceLogListener)

Notes

  • ServicePath.Restart() exits with code 1; configure your service manager to restart on this exit code
  • Log settings are automatically persisted to the data directory
  • File logs are automatically separated by date into subdirectories
  • ServiceLogListener does not support persistence (CanSave = false)

Thread Safety

  • ServicePath: Thread-safe (static properties and methods)
  • ServiceLogListener: Thread-safe for logging operations
  • ServiceSettingsBase: Not thread-safe; properties should be set during initialization

License

Part 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

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

GitHub repositories

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
Loading failed