初始化项目
This commit is contained in:
31
LogServer/ExceptionLogHelper.cs
Normal file
31
LogServer/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 LogServer
|
||||
{
|
||||
/// <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
LogServer/ExceptionLogMessage.cs
Normal file
124
LogServer/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 LogServer
|
||||
{
|
||||
/// <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());
|
||||
}
|
||||
}
|
||||
}
|
||||
63
LogServer/Log4NetHelper.cs
Normal file
63
LogServer/Log4NetHelper.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LogServer
|
||||
{
|
||||
public class Log4NetHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 写log
|
||||
/// </summary>
|
||||
/// <param name="logMessage"></param>
|
||||
public static void Log(string logMessage)
|
||||
{
|
||||
var log = LoggerFactory.CreateInstance(Log4NetType.Info.ToString());
|
||||
if (log.IsInfoEnabled)
|
||||
{
|
||||
log.Info(logMessage);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// debug
|
||||
/// </summary>
|
||||
/// <param name="logMessage"></param>
|
||||
public static void Debug(string logMessage)
|
||||
{
|
||||
var log = LoggerFactory.CreateInstance(Log4NetType.Info.ToString());
|
||||
if (log.IsInfoEnabled)
|
||||
{
|
||||
log.Info(logMessage);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// debug
|
||||
/// </summary>
|
||||
/// <param name="logMessage">消息</param>
|
||||
/// <param name="ex">异常</param>
|
||||
public static void Debug(string logMessage, Exception ex)
|
||||
{
|
||||
var log = LoggerFactory.CreateInstance(Log4NetType.Info.ToString());
|
||||
if (log.IsInfoEnabled)
|
||||
{
|
||||
log.Error(logMessage, ex);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// log
|
||||
/// </summary>
|
||||
/// <param name="logMessage">消息</param>
|
||||
/// <param name="ex">异常</param>
|
||||
public static void Log(string logMessage, Exception ex)
|
||||
{
|
||||
var log = LoggerFactory.CreateInstance(Log4NetType.Info.ToString());
|
||||
if (log.IsInfoEnabled)
|
||||
{
|
||||
log.Error(logMessage, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
32
LogServer/Log4NetType.cs
Normal file
32
LogServer/Log4NetType.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LogServer
|
||||
{
|
||||
/// <summary>
|
||||
/// 日志枚举类型
|
||||
/// </summary>
|
||||
public enum Log4NetType
|
||||
{
|
||||
/// <summary>
|
||||
/// 一般操作
|
||||
/// </summary>
|
||||
GeneralOperation,
|
||||
/// <summary>
|
||||
/// 异常日志
|
||||
/// </summary>
|
||||
Exception,
|
||||
/// <summary>
|
||||
/// 重要操作
|
||||
/// </summary>
|
||||
ImportantOperating,
|
||||
|
||||
/// <summary>
|
||||
/// 记录信息
|
||||
/// </summary>
|
||||
Info,
|
||||
}
|
||||
}
|
||||
59
LogServer/LogServer.csproj
Normal file
59
LogServer/LogServer.csproj
Normal file
@@ -0,0 +1,59 @@
|
||||
<?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>{B2171304-2FB0-4043-9C92-619E91462146}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>LogServer</RootNamespace>
|
||||
<AssemblyName>LogServer</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<Deterministic>true</Deterministic>
|
||||
<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="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="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" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
60
LogServer/LoggerFactory.cs
Normal file
60
LogServer/LoggerFactory.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using log4net;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LogServer
|
||||
{
|
||||
/// <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
LogServer/Properties/AssemblyInfo.cs
Normal file
36
LogServer/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// 有关程序集的一般信息由以下
|
||||
// 控制。更改这些特性值可修改
|
||||
// 与程序集关联的信息。
|
||||
[assembly: AssemblyTitle("LogServer")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("Organization")]
|
||||
[assembly: AssemblyProduct("LogServer")]
|
||||
[assembly: AssemblyCopyright("Copyright © Organization 2021")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// 将 ComVisible 设置为 false 会使此程序集中的类型
|
||||
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
|
||||
//请将此类型的 ComVisible 特性设置为 true。
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
||||
[assembly: Guid("b2171304-2fb0-4043-9c92-619e91462146")]
|
||||
|
||||
// 程序集的版本信息由下列四个值组成:
|
||||
//
|
||||
// 主版本
|
||||
// 次版本
|
||||
// 生成号
|
||||
// 修订号
|
||||
//
|
||||
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
|
||||
//通过使用 "*",如下所示:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
4
LogServer/packages.config
Normal file
4
LogServer/packages.config
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="log4net" version="2.0.8" targetFramework="net461" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user