Skip to content
This repository has been archived by the owner on Feb 3, 2024. It is now read-only.

Commit

Permalink
add log.
Browse files Browse the repository at this point in the history
  • Loading branch information
B1ackSand committed Sep 24, 2023
1 parent 718b84e commit b75587d
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 6 deletions.
84 changes: 83 additions & 1 deletion LiaoTian_Cup/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using LiaoTian_Cup.Helper;
using log4net;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
Expand All @@ -13,5 +15,85 @@ namespace LiaoTian_Cup
/// </summary>
public partial class App : Application
{
private static readonly ILog log = LogManager.GetLogger(typeof(App));
protected override void OnStartup(StartupEventArgs e)
{
LogHelper.InitLog4Net();
RegisterEvents();
base.OnStartup(e);
}

private void RegisterEvents()
{
//Task线程内未捕获异常处理事件
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;

//UI线程未捕获异常处理事件(UI主线程)
DispatcherUnhandledException += App_DispatcherUnhandledException;

//非UI线程未捕获异常处理事件(例如自己创建的一个子线程)
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}

//Task线程内未捕获异常处理事件
private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
try
{
if (e.Exception is Exception exception)
{
HandleException(exception);
}
}
catch (Exception ex)
{
HandleException(ex);
}
finally
{
e.SetObserved();
}
}

//非UI线程未捕获异常处理事件(例如自己创建的一个子线程)
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
if (e.ExceptionObject is Exception exception)
{
HandleException(exception);
}
}
catch (Exception ex)
{
HandleException(ex);
}
}

//UI线程未捕获异常处理事件(UI主线程)
private static void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
try
{
HandleException(e.Exception);
}
catch (Exception ex)
{
HandleException(ex);
}
finally
{
e.Handled = true;
}
}

//日志记录
private static void HandleException(Exception ex)
{
//记录堆栈和错误日志
LogHelper.WriteInfoLog(ex.ToString());
Current.Shutdown();
}
}
}
4 changes: 3 additions & 1 deletion LiaoTian_Cup/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
)]

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
2 changes: 1 addition & 1 deletion LiaoTian_Cup/Dictionary/I18n/Lang.resx
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
<value>you need to pick commander</value>
</data>
<data name="MainWindowTitle" xml:space="preserve">
<value>聊天杯 LiaoTian_Cup v0.2.0 - Programmed by B1ackSand</value>
<value>聊天杯 LiaoTian_Cup v0.2.1 - Programmed by B1ackSand</value>
</data>
<data name="ThreeMutatorsMode" xml:space="preserve">
<value>3 mutators mode</value>
Expand Down
2 changes: 1 addition & 1 deletion LiaoTian_Cup/Dictionary/I18n/Lang.zh.resx
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
<value>未选择指挥官</value>
</data>
<data name="MainWindowTitle" xml:space="preserve">
<value>聊天杯 LiaoTian_Cup v0.2.0 - Programmed by B1ackSand</value>
<value>聊天杯 LiaoTian_Cup v0.2.1 - Programmed by B1ackSand</value>
</data>
<data name="ThreeMutatorsMode" xml:space="preserve">
<value>3因子模式</value>
Expand Down
3 changes: 2 additions & 1 deletion LiaoTian_Cup/Helper/CSVKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ namespace LiaoTian_Cup.Helper
{
class CSVKit
{
// TODO 读写锁的问题存在
//两个实现
public static List<string[]> Csv2Dt(string filePath, List<string[]> list)
{
try
{
StreamReader reader = new StreamReader(filePath, Encoding.UTF8, false);
while (reader.Peek()>0)
while (reader.Peek() > 0)
{
string str = reader.ReadLine();
string[] split = str.Split(',');
Expand Down
39 changes: 39 additions & 0 deletions LiaoTian_Cup/Helper/LogHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using log4net.Config;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiaoTian_Cup.Helper
{
public class LogHelper
{
public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");

public static void InitLog4Net()
{
var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
XmlConfigurator.ConfigureAndWatch(logCfg);
loginfo.Info("----- 日志初始化 -----");
}

public static void WriteInfoLog(string info)
{
if (loginfo.IsInfoEnabled)
{
loginfo.Info(info);
}
}

public static void WriteErrLog(string info, Exception ex)
{
if (logerror.IsErrorEnabled)
{
logerror.Error(info, ex);
}
}
}
}
10 changes: 9 additions & 1 deletion LiaoTian_Cup/LiaoTian_Cup.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<UseWPF>true</UseWPF>
<ApplicationIcon>Resources\Logo\icon.ico</ApplicationIcon>
<Version>0.2.0</Version>
<Version>0.2.1</Version>
<Authors>B1ackSand</Authors>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
Expand All @@ -26,6 +26,7 @@
</ItemGroup>

<ItemGroup>
<None Remove="log4net.config" />
<None Remove="Resources\commander\凯拉克斯.png" />
<None Remove="Resources\commander\凯瑞甘.png" />
<None Remove="Resources\commander\德哈卡.png" />
Expand Down Expand Up @@ -165,6 +166,12 @@
<None Remove="Resources\自选突变列表.csv" />
</ItemGroup>

<ItemGroup>
<Content Include="log4net.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<Resource Include="Resources\factor\耐力挑战.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand All @@ -179,6 +186,7 @@

<ItemGroup>
<PackageReference Include="DeepCloner" Version="0.10.4" />
<PackageReference Include="log4net" Version="2.0.15" />
</ItemGroup>

<ItemGroup>
Expand Down
54 changes: 54 additions & 0 deletions LiaoTian_Cup/log4net.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<!--错误日志类-->
<logger name="logerror">
<!--日志类的名字-->
<level value="ALL" />
<!--定义记录的日志级别-->
<appender-ref ref="ErrorAppender" />
<!--记录到哪个介质中去-->
</logger>
<!--信息日志类-->
<logger name="loginfo">
<level value="ALL" />
<appender-ref ref="InfoAppender" />
</logger>
<!--错误日志附加介质-->
<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<!-- name属性指定其名称,type则是log4net.Appender命名空间的一个类的名称,意思是,指定使用哪种介质-->
<param name="File" value="Logs\\LogError\\" />
<!--日志输出到exe程序这个相对目录下-->
<param name="AppendToFile" value="true" />
<!--输出的日志不会覆盖以前的信息-->
<param name="MaxSizeRollBackups" value="100" />
<!--备份文件的个数-->
<param name="MaxFileSize" value="10240" />
<!--当个日志文件的最大大小-->
<param name="StaticLogFileName" value="false" />
<!--是否使用静态文件名-->
<param name="DatePattern" value="yyyyMMdd&quot;.htm&quot;" />
<!--日志文件名-->
<param name="RollingStyle" value="Date" />
<!--文件创建的方式,这里是以Date方式创建-->
<!--错误日志布局-->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="&lt;HR COLOR=red&gt;%n异常时间:%d [%t] &lt;BR&gt;%n异常级别:%-5p &lt;BR&gt;%n异 常 类:%c [%x] &lt;BR&gt;%n%m &lt;BR&gt;%n" />
</layout>
</appender>
<!--信息日志附加介质-->
<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Logs\\LogInfo\\" />
<param name="AppendToFile" value="true" />
<param name="MaxFileSize" value="10240" />
<param name="MaxSizeRollBackups" value="100" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMdd&quot;.htm&quot;" />
<param name="RollingStyle" value="Date" />
<!--信息日志布局-->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="&lt;HR COLOR=blue&gt;%n日志时间:%d [%t] &lt;BR&gt;%n日志级别:%-5p &lt;BR&gt;%n日 志 类:%c [%x] &lt;BR&gt;%n%m &lt;BR&gt;%n" />
</layout>
</appender>
</log4net>
</configuration>

0 comments on commit b75587d

Please sign in to comment.