添加项目文件。
This commit is contained in:
126
NX_LogUtility/LogHelper.cs
Normal file
126
NX_LogUtility/LogHelper.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace NX_LogUtility
|
||||
{
|
||||
public enum LogTypeEum
|
||||
{
|
||||
/// <summary>
|
||||
/// 程序运行日志
|
||||
/// </summary>
|
||||
Run,
|
||||
/// <summary>
|
||||
/// 程序错误日志
|
||||
/// </summary>
|
||||
Err,
|
||||
/// <summary>
|
||||
/// 数据库异常相关日志
|
||||
/// </summary>
|
||||
DbErr,
|
||||
/// <summary>
|
||||
/// OPC运行日志
|
||||
/// </summary>
|
||||
OPCRun,
|
||||
/// <summary>
|
||||
/// Socket运行日志
|
||||
/// </summary>
|
||||
SocketRun,
|
||||
/// <summary>
|
||||
/// Socket收发数据日志
|
||||
/// </summary>
|
||||
SocketData,
|
||||
}
|
||||
public static class LogHelper
|
||||
{
|
||||
#region 记录日志为文本文档形式
|
||||
/// <summary>
|
||||
/// 记录日志类
|
||||
/// </summary>
|
||||
/// <param name="Txt">日志内容</param>
|
||||
/// <param name="logType">日志类型(Run:程序运行日志; Err:程序错误日志; DbErr:数据库异常相关日志; OPCRun:OPC运行日志; SocketRun:Socket运行日志; SocErr:Socket异常日志。)</param>
|
||||
public static void WriteLog(string Txt, LogTypeEum logType)
|
||||
{
|
||||
//获取日志存放位置,年份不同,存放文件夹不同。
|
||||
string strDir = System.Environment.CurrentDirectory + "\\NX_CleanMac_Log\\" + DateTime.Now.ToString("yyyy") + "\\";
|
||||
//获取时间,年月日。
|
||||
string fileDate = DateTime.Now.ToString("yyyy-MM-dd");
|
||||
//获取时间,年月。
|
||||
string fileDate1 = DateTime.Now.ToString("yyyy-MM");
|
||||
//每一年的不同月份日志放在不同的文件夹,同一月份不同类型的日志放于同一个文件夹。
|
||||
string strDir1 = strDir + fileDate1 + "\\";
|
||||
//每一年的相同月份不同日期的日志放在不同的文件夹
|
||||
string strDir2 = strDir1 + fileDate + "\\";
|
||||
//不同的日志名称,日志文件不同
|
||||
string filePath = strDir2 + fileDate + "_" + logType + ".log";
|
||||
string DateString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
||||
string Str = "发生时间:" + DateString + "|" + Txt + "\r\n";
|
||||
WriteFile(strDir2, filePath, Str);
|
||||
}
|
||||
|
||||
private static readonly object lockobj = new object();
|
||||
/// <summary>
|
||||
/// 日志文件打印类
|
||||
/// </summary>
|
||||
/// <param name="strDir">日志文件存放的文件夹</param>
|
||||
/// <param name="filePath">日志文件存放路径</param>
|
||||
/// <param name="txt">日志内容</param>
|
||||
private static void WriteFile(string strDir, string filePath, string txt)
|
||||
{
|
||||
FileStream fs = null;
|
||||
StreamWriter sw = null;
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
lock (lockobj)
|
||||
{
|
||||
if (!Directory.Exists(strDir))//如果日志文件存放的文件夹不存在,就新建一个文件夹。
|
||||
{
|
||||
Directory.CreateDirectory(strDir);
|
||||
}
|
||||
|
||||
if (File.Exists(filePath))//日志文件存放路径存在,向日志文件追加记录。
|
||||
{
|
||||
fs = new FileStream(filePath, FileMode.Append);
|
||||
}
|
||||
else//日志文件存放路径不存在,创建日志文件。
|
||||
{
|
||||
fs = new FileStream(filePath, FileMode.Create);
|
||||
}
|
||||
|
||||
sw = new StreamWriter(fs);
|
||||
//开始写入
|
||||
sw.Write(txt);
|
||||
|
||||
//清空缓冲区
|
||||
sw.Flush();
|
||||
//关闭流
|
||||
sw.Close();
|
||||
fs.Close();
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (sw != null)
|
||||
sw.Close();
|
||||
if (fs != null)
|
||||
fs.Close();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
85
NX_LogUtility/NX_LogUtility.csproj
Normal file
85
NX_LogUtility/NX_LogUtility.csproj
Normal file
@@ -0,0 +1,85 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{925057D8-7EE8-42ED-8AA0-628AF82F10B2}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>NX_LogUtility</RootNamespace>
|
||||
<AssemblyName>NX_LogUtility</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<Deterministic>true</Deterministic>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<OutputPath>bin\x64\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<DebugType>full</DebugType>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
|
||||
<OutputPath>bin\x64\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<OutputPath>bin\x86\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<DebugType>full</DebugType>
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
|
||||
<OutputPath>bin\x86\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="LogHelper.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
36
NX_LogUtility/Properties/AssemblyInfo.cs
Normal file
36
NX_LogUtility/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// 有关程序集的一般信息由以下
|
||||
// 控制。更改这些特性值可修改
|
||||
// 与程序集关联的信息。
|
||||
[assembly: AssemblyTitle("NX_LogUtility")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("NX_LogUtility")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2020")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// 将 ComVisible 设置为 false 会使此程序集中的类型
|
||||
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
|
||||
//请将此类型的 ComVisible 特性设置为 true。
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
||||
[assembly: Guid("925057d8-7ee8-42ed-8aa0-628af82f10b2")]
|
||||
|
||||
// 程序集的版本信息由下列四个值组成:
|
||||
//
|
||||
// 主版本
|
||||
// 次版本
|
||||
// 生成号
|
||||
// 修订号
|
||||
//
|
||||
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
|
||||
//通过使用 "*",如下所示:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
Reference in New Issue
Block a user