初始化
This commit is contained in:
292
COMMON/ASLS.cs
Normal file
292
COMMON/ASLS.cs
Normal file
@@ -0,0 +1,292 @@
|
||||
using Aliyun.Api.LogService;
|
||||
using Aliyun.Api.LogService.Domain.Log;
|
||||
using Aliyun.Api.LogService.Domain.LogStore.Index;
|
||||
using Aliyun.Api.LogService.Infrastructure.Protocol;
|
||||
using Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace COMMON
|
||||
{
|
||||
/// <summary>
|
||||
/// 阿里云服务
|
||||
/// </summary>
|
||||
public class ASLS
|
||||
{
|
||||
//此处以深圳为例,其它地域请根据实际情况填写。
|
||||
private static string endpoint = "cn-shenzhen.log.aliyuncs.com";
|
||||
|
||||
// private static string accessKeyId = "LTAI4GKcpyML2ihVUe6bk1qj";
|
||||
private static string accessKeyId = "LTAI5tFDX4g74gJvEsdidRtD";
|
||||
|
||||
// private static string accessKeySecret = "IbeljP1LAuFYc0pGPvrX4AMAbrNhkj";
|
||||
private static string accessKeySecret = "mN7DDagPbjcKc5qFgHEIGvmasUsRF7";
|
||||
|
||||
//Project名称。
|
||||
private static string project = "rucs-project";
|
||||
|
||||
//Logstore名称。
|
||||
private static string logstore = "rucs-udp-log-db";
|
||||
|
||||
|
||||
|
||||
private static string logstore_ = "rucs-eventlogging-db";
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 主题
|
||||
/// </summary>
|
||||
private static string topic = "rucs-udp";
|
||||
|
||||
/// <summary>
|
||||
/// 创建日志服务Client。
|
||||
/// </summary>
|
||||
public ILogServiceClient client => BuildSimpleClient();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化 阿里云服务
|
||||
/// </summary>
|
||||
public async void Init()
|
||||
{
|
||||
|
||||
var data = await client.GetProjectAsync();
|
||||
|
||||
if (data.IsSuccess == true)
|
||||
{
|
||||
LogHelp.Warning(project + "项目已经存在~");
|
||||
}
|
||||
if (/*data == null ||*/ !data.IsSuccess)
|
||||
{
|
||||
//创建Project。
|
||||
var proRes = await client.CreateProjectAsync(project, "代码测试~");
|
||||
check(proRes);
|
||||
LogHelp.Warning("Create project success,await 30 S ~");
|
||||
Thread.Sleep(30 * 1000);
|
||||
|
||||
}
|
||||
|
||||
var data1 = await client.ListLogStoreAsync(logstore, 0);
|
||||
check(data1);
|
||||
if (data1.Result == null || data1.Result.Logstores.IndexOf(logstore) < 0)
|
||||
{
|
||||
//创建Logstore。
|
||||
var storeRes = await client.CreateLogStoreAsync(logstore, 90, 9);
|
||||
check(storeRes);
|
||||
LogHelp.Warning("Create logstore success,await 30 s~");
|
||||
Thread.Sleep(30 * 1000);
|
||||
//为Logstore创建索引。
|
||||
var Index = new Dictionary<String, IndexKeyInfo>() { };
|
||||
|
||||
Index.Add("id", new IndexLongKeyInfo() { DocValue = true });
|
||||
Index.Add("datetime", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("mac", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("msg_id", new IndexLongKeyInfo() { DocValue = true });
|
||||
Index.Add("result", new IndexLongKeyInfo() { DocValue = true });
|
||||
Index.Add("receipttime", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("packagetime", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("revdelaydatetime", new IndexLongKeyInfo() { DocValue = true });
|
||||
Index.Add("udp_cmd", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("prot_timeout", new IndexLongKeyInfo() { DocValue = true });
|
||||
Index.Add("remark", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("tcp_id", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("data_valid", new IndexLongKeyInfo() { DocValue = true });
|
||||
Index.Add("processing_timespent", new IndexLongKeyInfo() { DocValue = true });
|
||||
Index.Add("content_decrypt", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("content_encrypt", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("length", new IndexLongKeyInfo() { DocValue = true });
|
||||
Index.Add("retry_count", new IndexLongKeyInfo() { DocValue = true });
|
||||
Index.Add("sed_timeout", new IndexLongKeyInfo() { DocValue = true });
|
||||
Index.Add("rcv_timeout", new IndexLongKeyInfo() { DocValue = true });
|
||||
Index.Add("destination_port", new IndexLongKeyInfo() { DocValue = true });
|
||||
Index.Add("destination", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("source_port", new IndexLongKeyInfo() { DocValue = true });
|
||||
Index.Add("direction", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("source_ip", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("udp_id", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
|
||||
var indRes = await client.CreateIndexAsync(logstore, Index);
|
||||
check(indRes);
|
||||
LogHelp.Warning("Create Index success,await 30 s~");
|
||||
Thread.Sleep(30 * 1000);
|
||||
|
||||
}
|
||||
else
|
||||
LogHelp.Warning("logstore已经存在~");
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 写入阿里云服务
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<bool>write(string _logstore,params LogInfo[] log)
|
||||
{
|
||||
try
|
||||
{
|
||||
LogGroupInfo data = new LogGroupInfo();
|
||||
data.Logs = log;
|
||||
var indRes = await client.PostLogStoreLogsAsync(
|
||||
new PostLogsRequest(_logstore, data)
|
||||
{
|
||||
ProjectName = project
|
||||
}
|
||||
);
|
||||
check(indRes);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// 初始化 阿里云服务
|
||||
/// </summary>
|
||||
public async void Init_()
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = await client.GetProjectAsync();
|
||||
if (data.IsSuccess == true)
|
||||
{
|
||||
LogHelp.Warning(project + "项目已经存在~");
|
||||
Debug.Write(project + "项目已经存在~");
|
||||
}
|
||||
if (/*data == null ||*/ !data.IsSuccess)
|
||||
{
|
||||
//创建Project。
|
||||
var proRes = await client.CreateProjectAsync(project, "代码测试~");
|
||||
check(proRes);
|
||||
LogHelp.Warning("Create project success,await 30 S ~");
|
||||
Debug.Write("Create project success,await 30 S ~");
|
||||
Thread.Sleep(30 * 1000);
|
||||
}
|
||||
var data1 = await client.ListLogStoreAsync(logstore_, 0);
|
||||
check(data1);
|
||||
if (data1.Result == null || data1.Result.Logstores.IndexOf(logstore_) < 0)
|
||||
{
|
||||
//创建Logstore。
|
||||
var storeRes = await client.CreateLogStoreAsync(logstore_, 90, 9);
|
||||
check(storeRes);
|
||||
LogHelp.Warning("Create logstore success,await 30 s~");
|
||||
Debug.Write("Create logstore success,await 30 S ~");
|
||||
Thread.Sleep(30 * 1000);
|
||||
//为Logstore创建索引。
|
||||
var Index = new Dictionary<String, IndexKeyInfo>() { };
|
||||
Index.Add("event_type", new IndexLongKeyInfo() { DocValue = true });
|
||||
Index.Add("eventlogging_id", new IndexLongKeyInfo() { DocValue = true });
|
||||
Index.Add("registrar", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("input_datetime", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("remark", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("output_datatime", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("steprecording", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("info", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("role", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("mac", new IndexTextKeyInfo(new char[' ']) { DocValue = true });
|
||||
Index.Add("hotel_id", new IndexDoubleKeyInfo() { DocValue = true });
|
||||
Index.Add("hotel_old_id", new IndexDoubleKeyInfo() { DocValue = true });
|
||||
Index.Add("room_old_id", new IndexDoubleKeyInfo() { DocValue = true });
|
||||
Index.Add("room_id", new IndexDoubleKeyInfo() { DocValue = true });
|
||||
var indRes = await client.CreateIndexAsync(logstore_, Index);
|
||||
check(indRes);
|
||||
LogHelp.Warning("Create Index success,await 30 s~");
|
||||
Debug.Write("Create Index success,await 30 S ~");
|
||||
Thread.Sleep(30 * 1000);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
LogHelp.Warning("logstore已经存在~");
|
||||
Debug.Write("logstore已经存在~");
|
||||
}
|
||||
}
|
||||
catch (Exception EX)
|
||||
{
|
||||
Debug.Write(EX.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 查询 阿里云数据 udp
|
||||
/// </summary>
|
||||
/// <param name="sql"></param>
|
||||
/// <param name="start"></param>
|
||||
/// <param name="end"></param>
|
||||
/// <param name="line"></param>
|
||||
/// <param name="offset"></param>
|
||||
/// <param name="losg"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IList<IDictionary<string, string>>> GetLogs_(int losg, string sql, DateTime start = default , DateTime end = default, int line = 100, int offset = 0)
|
||||
{
|
||||
if(start == default)
|
||||
{
|
||||
start = ConfigEntity.Instance.STARTTIME;
|
||||
}
|
||||
if (end == default)
|
||||
{
|
||||
end = DateTime.Now;
|
||||
}
|
||||
var logstr = losg == 0 ? logstore : (losg==1? logstore_: "rucs-log-db");
|
||||
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
|
||||
|
||||
stopwatch.Start();
|
||||
if (end - start >= TimeSpan.FromDays(ConfigEntity.Instance.ASLS))
|
||||
start = end.AddDays(-ConfigEntity.Instance.ASLS);
|
||||
|
||||
sql = sql.TrimEnd();
|
||||
|
||||
IList<IDictionary<string, string>> RES = new List<IDictionary<string, string>>();
|
||||
|
||||
DateTimeOffset start_ = start;//.AddHours (-8);
|
||||
|
||||
DateTimeOffset end_ = end;//.AddHours(-8);
|
||||
try
|
||||
{
|
||||
var logsRes = await client.GetLogsAsync(logstr, start_, end_, topic, sql, line, offset, true);
|
||||
|
||||
check(logsRes);
|
||||
|
||||
return logsRes.Result.Logs;
|
||||
}
|
||||
catch (Exception EX)
|
||||
{
|
||||
LogHelp.Error(EX.ToString());
|
||||
RES = new List<IDictionary<string, string>>();
|
||||
}
|
||||
finally
|
||||
{
|
||||
stopwatch.Stop();
|
||||
LogHelp.Error($"{sql}---- 用时:" + (stopwatch.ElapsedMilliseconds / 1000.00) + "。</p>");
|
||||
}
|
||||
return RES;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// sls
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public ILogServiceClient BuildSimpleClient()
|
||||
=> LogServiceClientBuilders.HttpBuilder
|
||||
.Endpoint(endpoint, project)
|
||||
.Credential(accessKeyId, accessKeySecret)
|
||||
.Build();
|
||||
/// <summary>
|
||||
/// 检查结果
|
||||
/// </summary>
|
||||
/// <param name="res"></param>
|
||||
/// <exception cref="ApplicationException"></exception>
|
||||
public void check(IResponse res)
|
||||
{
|
||||
if (!res.IsSuccess)
|
||||
{
|
||||
throw new ApplicationException(res.Error != null ? res.Error.ErrorMessage : "请求错误~" + Newtonsoft.Json.JsonConvert.SerializeObject(res));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
22
COMMON/COMMON.csproj
Normal file
22
COMMON/COMMON.csproj
Normal file
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="log4net" Version="2.0.14" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.15" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.15" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
|
||||
<PackageReference Include="UtilsSharp" Version="2.5.0" />
|
||||
<PackageReference Include="UtilsSharp.Redis" Version="3.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Aliyun.Api.LogService\Aliyun.Api.LogService.csproj" />
|
||||
<ProjectReference Include="..\Models\Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
330
COMMON/FTPHELPER.cs
Normal file
330
COMMON/FTPHELPER.cs
Normal file
@@ -0,0 +1,330 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace COMMON
|
||||
{
|
||||
/// <summary>
|
||||
/// ftp 帮助类
|
||||
/// </summary>
|
||||
public static class FtpHelper
|
||||
{
|
||||
//基本设置
|
||||
static private string path => ConfigEntity.Instance.FTPPATH; //目标路径
|
||||
static private string ftpip => ConfigEntity.Instance.FTPIP; //ftp IP地址
|
||||
static private string username => ConfigEntity.Instance.FTPUSERNAME; //ftp用户名
|
||||
static private string password => ConfigEntity.Instance.FTPPASSWORD; //ftp密码
|
||||
|
||||
/// <summary>
|
||||
/// 获取ftp上面的文件和文件夹
|
||||
/// </summary>
|
||||
/// <param name="dir"></param>
|
||||
/// <returns></returns>
|
||||
public static string[] GetFileList(string dir)
|
||||
{
|
||||
StringBuilder result = new StringBuilder();
|
||||
FtpWebRequest request;
|
||||
|
||||
request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpip + Path.Combine(path, dir)));
|
||||
request.UseBinary = true;
|
||||
request.Credentials = new NetworkCredential(username, password);//设置用户名和密码
|
||||
request.Method = WebRequestMethods.Ftp.ListDirectory;
|
||||
request.UseBinary = true;
|
||||
|
||||
WebResponse response = request.GetResponse();
|
||||
StreamReader reader = new StreamReader(response.GetResponseStream());
|
||||
|
||||
string line = reader.ReadLine();
|
||||
while (line != null)
|
||||
{
|
||||
result.Append(line);
|
||||
result.Append("\n");
|
||||
line = reader.ReadLine();
|
||||
}
|
||||
// to remove the trailing '\n'
|
||||
result.Remove(result.ToString().LastIndexOf('\n'), 1);
|
||||
reader.Close();
|
||||
response.Close();
|
||||
return result.ToString().Split('\n');
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取文件大小
|
||||
/// </summary>
|
||||
/// <param name="file">ip服务器下的相对路径</param>
|
||||
/// <returns>文件大小</returns>
|
||||
public static int GetFileSize(string file)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
FtpWebRequest request;
|
||||
|
||||
string URL = ftpip + path +file.Replace("\\", "/");
|
||||
request = (FtpWebRequest)FtpWebRequest.Create(new Uri(URL));
|
||||
request.UseBinary = true;
|
||||
request.Credentials = new NetworkCredential(username, password);//设置用户名和密码
|
||||
request.Method = WebRequestMethods.Ftp.GetFileSize;
|
||||
|
||||
int dataLength = (int)request.GetResponse().ContentLength;
|
||||
|
||||
return dataLength;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 文件上传
|
||||
/// </summary>
|
||||
/// <param name="objPath">目标文件夹:服务器下的相对路径 不填为根目录</param>
|
||||
/// <param name="fs">目标文件夹:服务器下的相对路径 不填为根目录</param>
|
||||
/// <param name="newname">目标文件夹:服务器下的相对路径 不填为根目录</param>
|
||||
public static bool FileUpLoad(FileStream fs, string newname, string objPath = "")
|
||||
{
|
||||
string url = ftpip + path;
|
||||
if (objPath != "")
|
||||
url += objPath + "/";
|
||||
try
|
||||
{
|
||||
FtpWebRequest reqFTP = null;
|
||||
//待上传的文件 (全路径)
|
||||
try
|
||||
{
|
||||
using (fs)
|
||||
{
|
||||
long length = fs.Length;
|
||||
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url + newname));
|
||||
|
||||
//设置连接到FTP的帐号密码
|
||||
reqFTP.Credentials = new NetworkCredential(username, password);
|
||||
//设置请求完成后是否保持连接
|
||||
reqFTP.KeepAlive = false;
|
||||
//指定执行命令
|
||||
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
|
||||
//指定数据传输类型
|
||||
reqFTP.UseBinary = true;
|
||||
|
||||
using (Stream stream = reqFTP.GetRequestStream())
|
||||
{
|
||||
//设置缓冲大小
|
||||
int BufferLength = 5120;
|
||||
byte[] b = new byte[BufferLength];
|
||||
int i;
|
||||
while ((i = fs.Read(b, 0, BufferLength)) > 0)
|
||||
{
|
||||
stream.Write(b, 0, i);
|
||||
}
|
||||
Console.WriteLine("上传文件成功");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("上传文件失败错误为" + ex.Message);
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Console.WriteLine("上传文件失败错误为" + ex.Message);
|
||||
return false;
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 文件上传
|
||||
/// </summary>
|
||||
/// <param name="filePath">原路径(绝对路径)包括文件名</param>
|
||||
/// <param name="objPath">目标文件夹:服务器下的相对路径 不填为根目录</param>
|
||||
/// <param name="newname">目标文件夹:服务器下的相对路径 不填为根目录</param>
|
||||
public static bool FileUpLoad(string filePath,string newname, string objPath = "")
|
||||
{
|
||||
string url = ftpip + path;
|
||||
if (objPath != "")
|
||||
url += objPath + "/";
|
||||
|
||||
FtpWebRequest reqFTP = null;
|
||||
//待上传的文件 (全路径)
|
||||
|
||||
FileInfo fileInfo = new FileInfo(filePath);
|
||||
using (FileStream fs = fileInfo.OpenRead())
|
||||
{
|
||||
long length = fs.Length;
|
||||
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url + newname));
|
||||
|
||||
//设置连接到FTP的帐号密码
|
||||
reqFTP.Credentials = new NetworkCredential(username, password);
|
||||
//设置请求完成后是否保持连接
|
||||
reqFTP.KeepAlive = false;
|
||||
//指定执行命令
|
||||
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
|
||||
//指定数据传输类型
|
||||
reqFTP.UseBinary = true;
|
||||
|
||||
using (Stream stream = reqFTP.GetRequestStream())
|
||||
{
|
||||
//设置缓冲大小
|
||||
int BufferLength = 5120;
|
||||
byte[] b = new byte[BufferLength];
|
||||
int i;
|
||||
while ((i = fs.Read(b, 0, BufferLength)) > 0)
|
||||
{
|
||||
stream.Write(b, 0, i);
|
||||
}
|
||||
Console.WriteLine("上传文件成功");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除文件
|
||||
/// </summary>
|
||||
/// <param name="fileName">服务器下的相对路径 包括文件名</param>
|
||||
public static bool DeleteFileName(string fileName)
|
||||
{
|
||||
string URL = ftpip + path+ fileName.Replace("\\", "/");
|
||||
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(URL));
|
||||
// 指定数据传输类型
|
||||
reqFTP.UseBinary = true;
|
||||
// ftp用户名和密码
|
||||
reqFTP.Credentials = new NetworkCredential(username, password);
|
||||
// 默认为true,连接不会被关闭
|
||||
// 在一个命令之后被执行
|
||||
reqFTP.KeepAlive = false;
|
||||
// 指定执行什么命令
|
||||
reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
|
||||
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
|
||||
response.Close();
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新建目录 上一级必须先存在
|
||||
/// </summary>
|
||||
/// <param name="dirName">服务器下的相对路径</param>
|
||||
public static void MakeDir(string dirName)
|
||||
{
|
||||
|
||||
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpip +path+dirName));
|
||||
// 指定数据传输类型
|
||||
reqFTP.UseBinary = true;
|
||||
// ftp用户名和密码
|
||||
reqFTP.Credentials = new NetworkCredential(username, password);
|
||||
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
|
||||
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
|
||||
response.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除目录 上一级必须先存在
|
||||
/// </summary>
|
||||
/// <param name="dirName">服务器下的相对路径</param>
|
||||
public static void DelDir(string dirName)
|
||||
{
|
||||
|
||||
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpip + Path.Combine(path, dirName)));
|
||||
// ftp用户名和密码
|
||||
reqFTP.Credentials = new NetworkCredential(username, password);
|
||||
reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;
|
||||
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
|
||||
response.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从ftp服务器上获得文件夹列表
|
||||
/// </summary>
|
||||
/// <param name="RequedstPath">服务器下的相对路径</param>
|
||||
/// <returns></returns>
|
||||
public static List<string> GetDirctory(string RequedstPath)
|
||||
{
|
||||
List<string> strs = new List<string>();
|
||||
|
||||
|
||||
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpip + Path.Combine(path, RequedstPath)));
|
||||
// ftp用户名和密码
|
||||
reqFTP.Credentials = new NetworkCredential(username, password);
|
||||
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
|
||||
WebResponse response = reqFTP.GetResponse();
|
||||
StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名
|
||||
|
||||
string line = reader.ReadLine();
|
||||
while (line != null)
|
||||
{
|
||||
if (line.Contains("<DIR>"))
|
||||
{
|
||||
string msg = line.Substring(line.LastIndexOf("<DIR>") + 5).Trim();
|
||||
strs.Add(msg);
|
||||
}
|
||||
line = reader.ReadLine();
|
||||
}
|
||||
reader.Close();
|
||||
response.Close();
|
||||
return strs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从ftp服务器上获得文件列表
|
||||
/// </summary>
|
||||
/// <param name="RequedstPath">服务器下的相对路径</param>
|
||||
/// <returns></returns>
|
||||
public static List<string> GetFile(string RequedstPath)
|
||||
{
|
||||
List<string> strs = new List<string>();
|
||||
|
||||
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpip + Path.Combine(path, RequedstPath)));
|
||||
// ftp用户名和密码
|
||||
reqFTP.Credentials = new NetworkCredential(username, password);
|
||||
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
|
||||
WebResponse response = reqFTP.GetResponse();
|
||||
StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名
|
||||
|
||||
string line = reader.ReadLine();
|
||||
while (line != null)
|
||||
{
|
||||
if (!line.Contains("<DIR>"))
|
||||
{
|
||||
string msg = line.Substring(39).Trim();
|
||||
strs.Add(msg);
|
||||
}
|
||||
line = reader.ReadLine();
|
||||
}
|
||||
reader.Close();
|
||||
response.Close();
|
||||
return strs;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
63
COMMON/LogHelp.cs
Normal file
63
COMMON/LogHelp.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Cryptography;
|
||||
using System.IO;
|
||||
using Serilog;
|
||||
using Serilog.Events;
|
||||
using log4net;
|
||||
|
||||
namespace COMMON
|
||||
{
|
||||
public static class LogHelp
|
||||
{
|
||||
private static object locks = new object();
|
||||
private static string path = Directory.GetCurrentDirectory();
|
||||
public static string time = string.Empty;
|
||||
private static ILog log = LogManager.GetLogger("All");
|
||||
public static void Init()
|
||||
{
|
||||
if (time == string.Empty) {
|
||||
lock (locks)
|
||||
{
|
||||
if (time == string.Empty)
|
||||
{
|
||||
time = DateTime.Now.ToString("yyyy_MM_dd");
|
||||
string pathurl = path + "\\App_Data\\Log\\XC.log";
|
||||
Directory.CreateDirectory(path + "\\App_Data\\Log");
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.MinimumLevel.Warning()
|
||||
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
|
||||
.Enrich.FromLogContext()
|
||||
.WriteTo.Console()
|
||||
.WriteTo.File(pathurl, // 日志文件名
|
||||
outputTemplate: // 设置输出格式,显示详细异常信息
|
||||
@"{Timestamp:yyyy-MM-dd HH:mm-ss.fff }[{Level:u3}] {Message:lj}{NewLine}{Exception}",
|
||||
rollingInterval: RollingInterval.Day, // 日志按day保存
|
||||
rollOnFileSizeLimit: true, // 限制单个文件的最大长度
|
||||
encoding: Encoding.UTF8, // 文件字符编码
|
||||
retainedFileCountLimit: 10, // 最大保存文件数
|
||||
fileSizeLimitBytes: 5000 * 1024) // 最大单个文件长度 5m
|
||||
.CreateLogger();
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Error(string msg)
|
||||
{
|
||||
Init();
|
||||
Log.Error(msg);
|
||||
log.Error(msg);
|
||||
}
|
||||
|
||||
public static void Warning(string msg)
|
||||
{
|
||||
Init();
|
||||
Log.Warning(msg);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
24
COMMON/STARTTIME_HELP.cs
Normal file
24
COMMON/STARTTIME_HELP.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace COMMON
|
||||
{
|
||||
public class STARTTIME_HELP
|
||||
{
|
||||
#region 判断时间是否合理
|
||||
/// <summary>
|
||||
/// 判断起始时间时间是否合理
|
||||
/// </summary>
|
||||
/// <param name="date"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime GetSTARTTIME(DateTime date)
|
||||
{
|
||||
return date < ConfigEntity.Instance.STARTTIME? ConfigEntity.Instance.STARTTIME:date;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
23
COMMON/StringMac.cs
Normal file
23
COMMON/StringMac.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace COMMON
|
||||
{
|
||||
/// <summary>
|
||||
/// string mac扩展方法
|
||||
/// </summary>
|
||||
public static class StringMac
|
||||
{
|
||||
/// <summary>
|
||||
/// string 扩展 用于统一mac格式
|
||||
/// </summary>
|
||||
/// <param name="that"></param>
|
||||
public static string ToStringMac(this string that)
|
||||
{
|
||||
return that.Trim().ToUpper().Replace("34-D0", "").Replace("-","").ToLower();
|
||||
}
|
||||
}
|
||||
}
|
||||
53
COMMON/XC_Data.cs
Normal file
53
COMMON/XC_Data.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Models;
|
||||
using Models.Models.LOGDB;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace COMMON
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取ef
|
||||
/// </summary>
|
||||
public class XC_Data
|
||||
{
|
||||
private static object locks = new object();
|
||||
/// <summary>
|
||||
/// 获取主要数据库 每次都会创建一个实例 不建议使用
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static HotelServiceContext GetMinDataBase()
|
||||
{
|
||||
var optionsBuilder = new DbContextOptionsBuilder<HotelServiceContext>();
|
||||
if(ConfigEntity.Instance.DBTYPE == 1)
|
||||
{
|
||||
optionsBuilder.UseSqlServer(ConfigEntity.Instance.Connection);
|
||||
}
|
||||
else
|
||||
{
|
||||
optionsBuilder.UseMySQL(ConfigEntity.Instance.Connection);
|
||||
}
|
||||
return new HotelServiceContext(optionsBuilder.Options);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取日志数据库 每次都会创建一个实例 不建议使用
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static LOG_DBContext GetLogDataBase()
|
||||
{
|
||||
var optionsBuilder = new DbContextOptionsBuilder<LOG_DBContext>();
|
||||
if (ConfigEntity.Instance.DBTYPE == 1)
|
||||
{
|
||||
optionsBuilder.UseSqlServer(ConfigEntity.Instance.Connection_log);
|
||||
}
|
||||
else
|
||||
{
|
||||
optionsBuilder.UseMySQL(ConfigEntity.Instance.Connection_log);
|
||||
}
|
||||
return new LOG_DBContext(optionsBuilder.Options) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
74
COMMON/XC_Redis.cs
Normal file
74
COMMON/XC_Redis.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UtilsSharp.Redis;
|
||||
|
||||
namespace COMMON
|
||||
{
|
||||
public class XC_Redis
|
||||
{
|
||||
|
||||
public static XC_Redis Redis = new XC_Redis();
|
||||
private XC_Redis()
|
||||
{
|
||||
RedisCacheHelper.Initialization(new CSRedis.CSRedisClient(ConfigEntity.Instance.Redis));
|
||||
}
|
||||
public string fittleKey(string name)
|
||||
{
|
||||
return name.ToLower();
|
||||
}
|
||||
public T GetKey<T>(string name)
|
||||
{
|
||||
name = fittleKey(name);
|
||||
if (!string.IsNullOrEmpty(name) && RedisCacheHelper.IsExists(name))
|
||||
{
|
||||
return RedisCacheHelper.Get<T>(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
}
|
||||
|
||||
public bool SetKey<T>(string name, T VAL,int TIME = -1)
|
||||
{
|
||||
name = fittleKey(name);
|
||||
return RedisCacheHelper.Set(name, VAL, TIME);
|
||||
}
|
||||
|
||||
public T GET<T>(string name, Func<T> func, int TIME = -1)
|
||||
{
|
||||
name = fittleKey(name);
|
||||
var DATA = this.GetKey<T>(name);
|
||||
if (!RedisCacheHelper.IsExists(name) || DATA == null)
|
||||
{
|
||||
var data = func();
|
||||
SetKey(name,data, TIME);
|
||||
return data;
|
||||
}
|
||||
return DATA;
|
||||
}
|
||||
public bool Remove(string name)
|
||||
{
|
||||
name = fittleKey(name);
|
||||
if (string.IsNullOrEmpty(name) || !RedisCacheHelper.IsExists(name) || RedisCacheHelper.Remove(name)>0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool IsExists(string name)
|
||||
{
|
||||
name = fittleKey(name);
|
||||
if (string.IsNullOrEmpty(name) || !RedisCacheHelper.IsExists(name))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user