add - webapi服务,优化日志文件存储

dev
wenjy 2 months ago
parent c389b43689
commit 83499c572d

@ -46,9 +46,9 @@ namespace SlnMesnac.Serilog
#endregion
Log.Logger = new LoggerConfiguration().MinimumLevel.Information().WriteTo.Console()
.WriteTo.File(Path.Combine(logPath, "Info.log"), LogEventLevel.Information, fileSizeLimitBytes: 5 * 1024)
.WriteTo.File(Path.Combine(logPath, "Error.log"), LogEventLevel.Error, fileSizeLimitBytes: 5 * 1024)
.WriteTo.File(Path.Combine(logPath, "Warn.log"), LogEventLevel.Warning, fileSizeLimitBytes: 5 * 1024)
.WriteTo.File(Path.Combine(logPath, "Info.log"), LogEventLevel.Information)
.WriteTo.File(Path.Combine(logPath, "Error.log"), LogEventLevel.Error)
.WriteTo.File(Path.Combine(logPath, "Warn.log"), LogEventLevel.Warning)
//.WriteTo.File(Path.Combine(logPath, "Debug.log"), LogEventLevel.Debug, fileSizeLimitBytes: 5 * 1024)
.CreateLogger();
app.UseSerilogRequestLogging();

@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Text;
using TouchSocket.Rpc;
using TouchSocket.WebApi;
#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* (c) 2024 WenJY
* CLR4.0.30319.42000
* T14-GEN3-7895
* SlnMesnac.TouchSocket
* 649766cc-308e-4bf3-8d69-dea48ec40642
*
* WenJY
*
* 2024-09-04 10:51:54
* V1.0.0
*
*
*--------------------------------------------------------------------
*
*
*
*
* V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.TouchSocket
{
public class ApiServer: RpcServer
{
public delegate void RefreshScanInfoInCenterStart();
/// <summary>
/// 入库开始事件刷新
/// </summary>
public event RefreshScanInfoInCenterStart RefreshScanInfoInCenterStartEvent;
public delegate void RefreshScanInfoInCenterStop();
/// <summary>
/// 入库结束事件刷新
/// </summary>
public event RefreshScanInfoInCenterStop RefreshScanInfoInCenterStopEvent;
public delegate void RefreshScanInfoOutCenterStart();
/// <summary>
/// 出库开始事件刷新
/// </summary>
public event RefreshScanInfoOutCenterStart RefreshScanInfoOutCenterStartEvent;
public delegate void RefreshScanInfoOutCenterStop();
/// <summary>
/// 出库结束事件刷新
/// </summary>
public event RefreshScanInfoOutCenterStop RefreshScanInfoOutCenterStopEvent;
/// <summary>
/// 入库开始
/// </summary>
/// <param name="messageHeader"></param>
/// <returns></returns>
[EnableCors("cors")]
[WebApi(HttpMethodType.POST)]
public object getScanInfoInCenterStart(string messageHeader)
{
RefreshScanInfoInCenterStartEvent?.Invoke();
return true;
}
/// <summary>
/// 入库结束
/// </summary>
/// <param name="messageHeader"></param>
/// <returns></returns>
[EnableCors("cors")]
[WebApi(HttpMethodType.POST)]
public object getScanInfoInCenterStop(string messageHeader)
{
RefreshScanInfoInCenterStopEvent?.Invoke();
return true;
}
/// <summary>
/// 出库开始
/// </summary>
/// <param name="messageHeader"></param>
/// <returns></returns>
[WebApi(HttpMethodType.POST)]
public object getScanInfoOutCenterStart(string messageHeader)
{
RefreshScanInfoOutCenterStartEvent?.Invoke();
return true;
}
/// <summary>
/// 出库结束
/// </summary>
/// <param name="messageHeader"></param>
/// <returns></returns>
[WebApi(HttpMethodType.POST)]
public object getScanInfoOutCenterStop(string messageHeader)
{
RefreshScanInfoOutCenterStopEvent?.Invoke();
return true;
}
}
}

@ -6,7 +6,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="TouchSocket" Version="2.0.0-beta.270" />
<PackageReference Include="TouchSocket" Version="2.0.17" />
<PackageReference Include="TouchSocket.WebApi" Version="2.0.17" />
<PackageReference Include="TouchSocket.WebApi.Swagger" Version="2.0.17" />
</ItemGroup>
<ItemGroup>

@ -39,6 +39,9 @@ namespace SlnMesnac.TouchSocket
{
var _server = app.ApplicationServices.GetService<TcpServer>();
_server.Init(20108);
var _apiServer = app.ApplicationServices.GetService<WebApiServer>();
_apiServer.Init();
return app;
}
}

@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Http;
using TouchSocket.Rpc;
using TouchSocket.Sockets;
using TouchSocket.WebApi.Swagger;
#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* (c) 2024 WenJY
* CLR4.0.30319.42000
* T14-GEN3-7895
* SlnMesnac.TouchSocket
* 4e47989b-9d43-426e-b67a-529de3b1b0e8
*
* WenJY
*
* 2024-09-04 10:51:29
* V1.0.0
*
*
*--------------------------------------------------------------------
*
*
*
*
* V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.TouchSocket
{
public class WebApiServer
{
private ApiServer _apiServer;
public WebApiServer(ApiServer apiServer)
{
_apiServer = apiServer;
}
public void Init()
{
try
{
var service = new HttpService();
service.Setup(new TouchSocketConfig()
.SetListenIPHosts(7789)
.ConfigureContainer(a =>
{
a.AddRpcStore(store =>
{
store.RegisterServer<ApiServer>(_apiServer);//注册服务
});
a.AddCors(corsOption =>
{
corsOption.Add("cors", corsBuilder =>
{
corsBuilder.AllowAnyMethod()
.AllowAnyOrigin();
});
});
a.AddLogger(logger =>
{
logger.AddConsoleLogger();
logger.AddFileLogger();
});
})
.ConfigurePlugins(a =>
{
a.UseCheckClear();
a.Add<AuthenticationPlugin>();
a.UseWebApi()
.ConfigureConverter(converter =>
{
converter.AddJsonSerializerFormatter(new Newtonsoft.Json.JsonSerializerSettings() { Formatting = Newtonsoft.Json.Formatting.None });
});
a.UseSwagger();//使用Swagger页面
//.UseLaunchBrowser();
a.UseDefaultHttpServicePlugin();
}));
service.Start();
Console.WriteLine("以下连接用于测试webApi");
Console.WriteLine($"使用http://127.0.0.1:7789/swagger/index.html");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
//Console.ReadLine();
}
}
internal class AuthenticationPlugin : PluginBase, IHttpPlugin
{
public async Task OnHttpRequest(IHttpSocketClient client, HttpContextEventArgs e)
{
await e.InvokeNext();
}
}
}

@ -12,6 +12,7 @@ using SlnMesnac.Rfid;
using SlnMesnac.Ioc;
using SlnMesnac.Plc;
using SlnMesnac.Extensions;
using SlnMesnac.TouchSocket;
namespace SlnMesnac.WPF
{
@ -71,6 +72,8 @@ namespace SlnMesnac.WPF
//启用Serilog中间件
app.UseSerilogExtensions();
app.UseTouchSocketExtensions();
app.UseRouting();
app.UseAuthorization();

Loading…
Cancel
Save