首页 > 其他分享 >dotnet 扩展Service方法 secs4net

dotnet 扩展Service方法 secs4net

时间:2024-01-23 22:35:01浏览次数:27  
标签:Service void msg services dotnet using logger public secs4net

secs_learn/Program.cs

using DeviceWorkerService;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

Host.CreateDefaultBuilder(args)
    .ConfigureServices((hostContext, services) =>
    {
        services.AddSecs4Net<DeviceLogger>(hostContext.Configuration);
    }).Build().Run();

secs_learn/DeviceWorkerService.cs

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Secs4Net;
using System.Diagnostics.CodeAnalysis;

namespace DeviceWorkerService;

public static class ServiceProvider
{
    public static IServiceCollection AddSecs4Net<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TLogger>(this IServiceCollection services, IConfiguration configuration)
        where TLogger : class, ISecsGemLogger
    {
        var configSection = configuration.GetSection("secs4net");
        services.Configure<SecsGemOptions>(configSection);
        services.AddSingleton<ISecsConnection, HsmsConnection>();
        services.AddSingleton<ISecsGem, SecsGem>();
        services.AddSingleton<ISecsGemLogger, TLogger>();
        return services;
    }
}

secs_learn/DeviceLogger.cs

using Microsoft.Extensions.Logging;
using Secs4Net;
using Secs4Net.Sml;
using System;

namespace DeviceWorkerService;

internal sealed class DeviceLogger(ILogger<DeviceLogger> logger) : ISecsGemLogger
{
    public void MessageIn(SecsMessage msg, int id) => logger.LogTrace($"<-- [0x{id:X8}] {msg.ToSml()}");
    public void MessageOut(SecsMessage msg, int id) => logger.LogTrace($"--> [0x{id:X8}] {msg.ToSml()}");
    public void Debug(string msg) => logger.LogDebug(msg);
    public void Info(string msg) => logger.LogInformation(msg);
    public void Warning(string msg) => logger.LogWarning(msg);
    public void Error(string msg, SecsMessage? message, Exception? ex) => logger.LogError(ex, $"{msg} {message}\n");
}

标签:Service,void,msg,services,dotnet,using,logger,public,secs4net
From: https://www.cnblogs.com/zhuoss/p/17983582

相关文章

  • dotnet注册服务 secs4net secs
    secs_learn/Program.cs此文件是应用程序的入口点,用于配置和启动主机。使用Host.CreateDefaultBuilder(args)创建一个默认配置的主机构建器,自动加载环境变量、配置文件等设置。.ConfigureServices(...)方法中,通过services.AddHostedService<DeviceWorker>()注册DeviceWor......
  • error 'vpxservicesMoServiceDirectory'
    error'vpxservicesMoServiceDirectory'][ServiceDirectory]Instanceentrymissingservices:65bf45a事故说明 :当时由于venter环境的win2008磁盘爆满 ,后期发现vcenterclient无法登录 ,过后发现VMwareVirtualCenterServerService为停止状态 ,手动重启失败 ,经......
  • 如何为Azure Kubernetes Services启用Internal Loadbalancer
    如何为AzureKubernetesServices启用InternalLoadbalancer熟悉AzureKubernetesServices(AKS)的小伙伴都知道,默认情况下,当我们创建AzureKubernetesServices群集时,创建的都是Public的AKS群集,也就是可以提供Internet访问的AKS群集。PublicAKS群集会默认附带一个Public类型的Load......
  • C# RSA加密-RSACryptoServiceProvider
    1.获取公钥和私钥(XML格式)///<summary>///获取RSA密钥///下标0为私钥///下标1为公钥///</summary>///<returns></returns>publicstaticstring[]GetRSAKey(){RSACry......
  • sudo: unable to resolve host xxxx: Name or service not known
    前言在Linux环境中,我使用sudo执行命令,发生报错:sudo:unabletoresolvehostxxxx:Nameorservicenotknown解决这个错误通常发生在更改主机名后,使用sudo命令时出现问题。sudo命令会尝试解析主机名,但如果无法解析,就会出现"sudo:unabletoresolvehost"的错误。然而......
  • k8s之基于metallb实现LoadBalancer型Service
    一、实验说明1、实验目的基于metallb实现kubernetes的LoadBalancer型Service。2、环境说明VMwareWorkstation安装三台虚拟机,安装K8S集群,网络模式NAT模式。master11.0.1.131node0111.0.1.132node0211.0.1.133oot@master:/home/user#kubectlgetnodesNAMESTATU......
  • Servlet系列:生命周期(init、 service、destroy)详解
    Servlet的生命周期是由Web容器(如Tomcat)管理的,包括以下三个阶段:加载和实例化:当Web应用程序启动时,Web容器会加载和实例化Servlet。加载和实例化过程可以在应用程序启动时自动完成,也可以通过Servlet的名称手动加载。在实例化Servlet后,Web容器会调用其init()方法进行初始化。处理请......
  • 将 .NET 8应用 以 dotnet publish 创建容器镜像并结合 Github Actions 部署到 Azure
    介绍.NET8无需DockerFile即可为.NET应用创建docker映像的新方法,我将使用dotnetpublish将.NET应用容器化,在本文中,我将分享我如何为.NET8的项目创建一个简单的ci/cd的经验。它包括2个主题:创建用于生成.NET应用并将其发布到Azure的GitHub工作流如何使用do......
  • dotnet 多数据库 sqlite efcore model和entity区别 一对多 多对一 多对多
    efcore-multi-db/MultiDb.slnMicrosoftVisualStudioSolutionFile,FormatVersion12.00#VisualStudio15VisualStudioVersion=15.0.27130.2024MinimumVisualStudioVersion=10.0.40219.1Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}")="......
  • dotnet efcore 多数据库 使用
    efcore的使用依赖包efcore-multi-db/MultiDb.Two/MultiDb.Two.csproj<ProjectSdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net7.0</TargetFramework></PropertyGroup><ItemGroup><Packa......