初始化
This commit is contained in:
31
Face.Log4Net/ExceptionLogHelper.cs
Normal file
31
Face.Log4Net/ExceptionLogHelper.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Face.Log4Net
|
||||
{
|
||||
/// <summary>
|
||||
/// 一般操作产生的日志
|
||||
/// </summary>
|
||||
public class ExceptionLogHelper
|
||||
{
|
||||
public static void Log(ExceptionLogMessage logMessage)
|
||||
{
|
||||
var log = LoggerFactory.CreateInstance(Log4NetType.Exception.ToString());
|
||||
if (log.IsErrorEnabled)
|
||||
{
|
||||
log.Error(logMessage);
|
||||
}
|
||||
}
|
||||
public static void Log(ExceptionLogMessage logMessage, Exception exception)
|
||||
{
|
||||
var log = LoggerFactory.CreateInstance(Log4NetType.Exception.ToString());
|
||||
if (log.IsErrorEnabled)
|
||||
{
|
||||
log.Error(logMessage, exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
124
Face.Log4Net/ExceptionLogMessage.cs
Normal file
124
Face.Log4Net/ExceptionLogMessage.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using log4net.Core;
|
||||
using log4net.Layout.Pattern;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Face.Log4Net
|
||||
{
|
||||
/// <summary>
|
||||
/// 一般操作
|
||||
/// </summary>
|
||||
public class ExceptionLogMessage
|
||||
{
|
||||
/// <summary>
|
||||
/// 操作人
|
||||
/// </summary>
|
||||
public string Operationer { get; set; }
|
||||
/// <summary>
|
||||
/// 客户端IP
|
||||
/// </summary>
|
||||
public string IP { get; set; }
|
||||
/// <summary>
|
||||
/// 浏览器请求头
|
||||
/// </summary>
|
||||
public string Header { get; set; }
|
||||
/// <summary>
|
||||
/// 请求地址
|
||||
/// </summary>
|
||||
public string Url { get; set; }
|
||||
/// <summary>
|
||||
/// 功能点名称
|
||||
/// </summary>
|
||||
public string ActionName { get; set; }
|
||||
|
||||
public Exception exception { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("功能点:{0} \n操作者:{1} \nIP:{2} \n头部信息:{3} \n来自页面:{4} 错误信息:{5}", ActionName, Operationer, IP, Header, Url, exception.ToString());
|
||||
}
|
||||
}
|
||||
public class ExceptionLayout : log4net.Layout.PatternLayout
|
||||
{
|
||||
public ExceptionLayout()
|
||||
{
|
||||
this.AddConverter("exceptionMessage", typeof(ExceptionPatternConverter));
|
||||
this.AddConverter("ActionName", typeof(ActionNameExceptionPatternConverter));
|
||||
this.AddConverter("Operationer", typeof(OperationerExceptionPatternConverter));
|
||||
this.AddConverter("IP", typeof(IPExceptionPatternConverter));
|
||||
this.AddConverter("Header", typeof(HeaderExceptionPatternConverter));
|
||||
this.AddConverter("Url", typeof(UrlExceptionPatternConverter));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal sealed class ActionNameExceptionPatternConverter : PatternLayoutConverter
|
||||
{
|
||||
override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
|
||||
{
|
||||
ExceptionLogMessage logMessage = loggingEvent.MessageObject as ExceptionLogMessage;
|
||||
|
||||
if (logMessage != null)
|
||||
writer.Write(logMessage.ActionName);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class OperationerExceptionPatternConverter : PatternLayoutConverter
|
||||
{
|
||||
override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
|
||||
{
|
||||
ExceptionLogMessage logMessage = loggingEvent.MessageObject as ExceptionLogMessage;
|
||||
|
||||
if (logMessage != null)
|
||||
writer.Write(logMessage.Operationer);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class IPExceptionPatternConverter : PatternLayoutConverter
|
||||
{
|
||||
override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
|
||||
{
|
||||
ExceptionLogMessage logMessage = loggingEvent.MessageObject as ExceptionLogMessage;
|
||||
|
||||
if (logMessage != null)
|
||||
writer.Write(logMessage.IP);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class HeaderExceptionPatternConverter : PatternLayoutConverter
|
||||
{
|
||||
override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
|
||||
{
|
||||
ExceptionLogMessage logMessage = loggingEvent.MessageObject as ExceptionLogMessage;
|
||||
|
||||
if (logMessage != null)
|
||||
writer.Write(logMessage.Header);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class UrlExceptionPatternConverter : PatternLayoutConverter
|
||||
{
|
||||
override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
|
||||
{
|
||||
ExceptionLogMessage logMessage = loggingEvent.MessageObject as ExceptionLogMessage;
|
||||
|
||||
if (logMessage != null)
|
||||
writer.Write(logMessage.Url);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class ExceptionPatternConverter : PatternLayoutConverter
|
||||
{
|
||||
override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
|
||||
{
|
||||
ExceptionLogMessage logMessage = loggingEvent.MessageObject as ExceptionLogMessage;
|
||||
|
||||
if (logMessage != null)
|
||||
writer.Write(logMessage.exception.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
77
Face.Log4Net/Face.Log4Net.csproj
Normal file
77
Face.Log4Net/Face.Log4Net.csproj
Normal file
@@ -0,0 +1,77 @@
|
||||
<?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>{5500F9DD-89D9-40F3-B62F-90C6B633D55A}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>Face.Log4Net</RootNamespace>
|
||||
<AssemblyName>Face.Log4Net</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<Deterministic>true</Deterministic>
|
||||
<SccProjectName>SAK</SccProjectName>
|
||||
<SccLocalPath>SAK</SccLocalPath>
|
||||
<SccAuxPath>SAK</SccAuxPath>
|
||||
<SccProvider>SAK</SccProvider>
|
||||
<TargetFrameworkProfile />
|
||||
</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>
|
||||
</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>
|
||||
<ItemGroup>
|
||||
<Reference Include="log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\log4net.2.0.8\lib\net45-full\log4net.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.13.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Text.Encodings.Web, Version=4.0.3.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.Text.Encodings.Web.4.5.1\lib\netstandard2.0\System.Text.Encodings.Web.dll</HintPath>
|
||||
</Reference>
|
||||
<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" />
|
||||
<Reference Include="TencentCloudCommon, Version=3.0.902.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\TencentCloudSDK.Common.3.0.902\lib\net45\TencentCloudCommon.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="TencentCloudSms, Version=3.0.902.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\TencentCloudSDK.Sms.3.0.902\lib\net45\TencentCloudSms.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ExceptionLogHelper.cs" />
|
||||
<Compile Include="ExceptionLogMessage.cs" />
|
||||
<Compile Include="Log4NetHelper.cs" />
|
||||
<Compile Include="Log4NetType.cs" />
|
||||
<Compile Include="LoggerFactory.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config">
|
||||
<SubType>Designer</SubType>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
10
Face.Log4Net/Face.Log4Net.csproj.vspscc
Normal file
10
Face.Log4Net/Face.Log4Net.csproj.vspscc
Normal file
@@ -0,0 +1,10 @@
|
||||
""
|
||||
{
|
||||
"FILE_VERSION" = "9237"
|
||||
"ENLISTMENT_CHOICE" = "NEVER"
|
||||
"PROJECT_FILE_RELATIVE_PATH" = ""
|
||||
"NUMBER_OF_EXCLUDED_FILES" = "0"
|
||||
"ORIGINAL_PROJECT_FILE_PATH" = ""
|
||||
"NUMBER_OF_NESTED_PROJECTS" = "0"
|
||||
"SOURCE_CONTROL_SETTINGS_PROVIDER" = "PROVIDER"
|
||||
}
|
||||
46
Face.Log4Net/Log4NetHelper.cs
Normal file
46
Face.Log4Net/Log4NetHelper.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Face.Log4Net
|
||||
{
|
||||
public class Log4NetHelper
|
||||
{
|
||||
public static void Log(string logMessage)
|
||||
{
|
||||
var log = LoggerFactory.CreateInstance(Log4NetType.Info.ToString());
|
||||
if (log.IsInfoEnabled)
|
||||
{
|
||||
log.Info(logMessage);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Debug(string logMessage)
|
||||
{
|
||||
var log = LoggerFactory.CreateInstance(Log4NetType.Info.ToString());
|
||||
if (log.IsInfoEnabled)
|
||||
{
|
||||
log.Info(logMessage);
|
||||
}
|
||||
}
|
||||
public static void Debug(string logMessage, Exception ex)
|
||||
{
|
||||
var log = LoggerFactory.CreateInstance(Log4NetType.Info.ToString());
|
||||
if (log.IsInfoEnabled)
|
||||
{
|
||||
log.Error(logMessage, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Log(string logMessage, Exception ex)
|
||||
{
|
||||
var log = LoggerFactory.CreateInstance(Log4NetType.Info.ToString());
|
||||
if (log.IsInfoEnabled)
|
||||
{
|
||||
log.Error(logMessage, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
Face.Log4Net/Log4NetHelper.rar
Normal file
BIN
Face.Log4Net/Log4NetHelper.rar
Normal file
Binary file not shown.
34
Face.Log4Net/Log4NetType.cs
Normal file
34
Face.Log4Net/Log4NetType.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Face.Log4Net
|
||||
{
|
||||
/// <summary>
|
||||
/// 日志枚举类型
|
||||
/// </summary>
|
||||
public enum Log4NetType
|
||||
{
|
||||
/// <summary>
|
||||
/// 一般操作
|
||||
/// </summary>
|
||||
GeneralOperation,
|
||||
/// <summary>
|
||||
/// 异常日志
|
||||
/// </summary>
|
||||
Exception,
|
||||
/// <summary>
|
||||
/// 重要操作
|
||||
/// </summary>
|
||||
ImportantOperating,
|
||||
|
||||
/// <summary>
|
||||
/// 记录信息
|
||||
/// </summary>
|
||||
Info,
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
61
Face.Log4Net/LoggerFactory.cs
Normal file
61
Face.Log4Net/LoggerFactory.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using log4net;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Face.Log4Net
|
||||
{
|
||||
/// <summary>
|
||||
/// Logger 工厂
|
||||
/// </summary>
|
||||
class LoggerFactory
|
||||
{
|
||||
private static Dictionary<string, ILog> dicLog = new Dictionary<string, ILog>();
|
||||
|
||||
/// <summary>
|
||||
/// 创建实例 ,默认读取App_Data 下的log4net.config文件
|
||||
/// </summary>
|
||||
/// <param name="loggerName"></param>
|
||||
/// <returns></returns>
|
||||
public static ILog CreateInstance(string loggerName)
|
||||
{
|
||||
return CreateInstance(loggerName, null);
|
||||
}
|
||||
/// <summary>
|
||||
/// 创建实例
|
||||
/// </summary>
|
||||
/// <param name="loggerName"></param>
|
||||
/// <param name="configFilename">log4net.config文件路径</param>
|
||||
/// <returns></returns>
|
||||
public static ILog CreateInstance(string loggerName, string configFilename)
|
||||
{
|
||||
if (!dicLog.ContainsKey(loggerName))
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(configFilename))
|
||||
{
|
||||
configFilename = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).FullName + "\\App_Data\\configs\\log4net.config";
|
||||
}
|
||||
|
||||
log4net.Config.XmlConfigurator.Configure(new FileInfo(configFilename));
|
||||
//从配置文件中读取Logger对象
|
||||
//WebLogger 里面的配置信息是用来将日志录入到数据库的
|
||||
//做为扩展 做判断来确定日志的记录形式,数据库也好,txt文档也好,控制台程序也好。
|
||||
var log = log4net.LogManager.GetLogger(loggerName);
|
||||
|
||||
//防止并发出错
|
||||
if (!dicLog.ContainsKey(loggerName))
|
||||
{
|
||||
dicLog.Add(loggerName, log);
|
||||
}
|
||||
return log;
|
||||
}
|
||||
else
|
||||
{
|
||||
return dicLog[loggerName];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
36
Face.Log4Net/Properties/AssemblyInfo.cs
Normal file
36
Face.Log4Net/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// 有关程序集的一般信息由以下
|
||||
// 控制。更改这些特性值可修改
|
||||
// 与程序集关联的信息。
|
||||
[assembly: AssemblyTitle("Face.Log4Net")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("Face.Log4Net")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2021")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// 将 ComVisible 设置为 false 会使此程序集中的类型
|
||||
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
|
||||
//请将此类型的 ComVisible 特性设置为 true。
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
||||
[assembly: Guid("5500f9dd-89d9-40f3-b62f-90c6b633d55a")]
|
||||
|
||||
// 程序集的版本信息由下列四个值组成:
|
||||
//
|
||||
// 主版本
|
||||
// 次版本
|
||||
// 生成号
|
||||
// 修订号
|
||||
//
|
||||
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
|
||||
//通过使用 "*",如下所示:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
27
Face.Log4Net/app.config
Normal file
27
Face.Log4Net/app.config
Normal file
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<runtime>
|
||||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0" />
|
||||
</dependentAssembly>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
|
||||
</dependentAssembly>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
|
||||
</dependentAssembly>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="System.Text.Encodings.Web" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-4.0.5.1" newVersion="4.0.5.1" />
|
||||
</dependentAssembly>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
|
||||
</dependentAssembly>
|
||||
</assemblyBinding>
|
||||
</runtime>
|
||||
</configuration>
|
||||
8
Face.Log4Net/packages.config
Normal file
8
Face.Log4Net/packages.config
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="log4net" version="2.0.8" targetFramework="net461" />
|
||||
<package id="Newtonsoft.Json" version="13.0.2" targetFramework="net47" />
|
||||
<package id="System.Text.Encodings.Web" version="4.5.1" targetFramework="net47" />
|
||||
<package id="TencentCloudSDK.Common" version="3.0.902" targetFramework="net47" />
|
||||
<package id="TencentCloudSDK.Sms" version="3.0.902" targetFramework="net47" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user