替换日志,日志写入文件
This commit is contained in:
84
精工涂胶检测项目/CVE.LogViewer/CVE.LogViewer.csproj
Normal file
84
精工涂胶检测项目/CVE.LogViewer/CVE.LogViewer.csproj
Normal file
@@ -0,0 +1,84 @@
|
||||
<?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>{766CF29F-3629-470A-AEC7-D2AEAD685484}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>CVE.LogViewer</RootNamespace>
|
||||
<AssemblyName>CVE.LogViewer</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>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<OutputPath>bin\x64\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<DebugType>full</DebugType>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
|
||||
<OutputPath>bin\x64\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="log4net, Version=2.0.14.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\log4net.2.0.14\lib\net45\log4net.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Web" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<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="DataGridViewAppender.cs" />
|
||||
<Compile Include="Helper.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="LogConfig.xml">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
<SubType>Designer</SubType>
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
144
精工涂胶检测项目/CVE.LogViewer/DataGridViewAppender.cs
Normal file
144
精工涂胶检测项目/CVE.LogViewer/DataGridViewAppender.cs
Normal file
@@ -0,0 +1,144 @@
|
||||
using log4net.Appender;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace CVE.LogViewer
|
||||
{
|
||||
class DataGridViewAppender : AppenderSkeleton
|
||||
{
|
||||
private DataGridView _dataGridView;
|
||||
public DataGridView AppenderControl
|
||||
{
|
||||
get
|
||||
{
|
||||
return _dataGridView;
|
||||
}
|
||||
set
|
||||
{
|
||||
_dataGridView = value;
|
||||
}
|
||||
}
|
||||
public string FormName { get; set; }
|
||||
public string ListViewName { get; set; }
|
||||
/// <summary>
|
||||
/// 查找控件
|
||||
/// </summary>
|
||||
/// <param name="root"></param>
|
||||
/// <param name="ListViewName"></param>
|
||||
/// <returns></returns>
|
||||
private Control FindControlRecursive(Control root, string ListViewName)
|
||||
{
|
||||
if (root.Name == ListViewName) return root;
|
||||
foreach (Control c in root.Controls)
|
||||
{
|
||||
Control t = FindControlRecursive(c, ListViewName);
|
||||
if (t != null) return t;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示日志
|
||||
/// </summary>
|
||||
/// <param name="loggingEvent"></param>
|
||||
protected override void Append(log4net.Core.LoggingEvent loggingEvent)
|
||||
{
|
||||
//验证控件是否存在
|
||||
if (_dataGridView == null)
|
||||
{
|
||||
if (String.IsNullOrEmpty(FormName) ||
|
||||
String.IsNullOrEmpty(ListViewName))
|
||||
return;
|
||||
|
||||
Form form = Application.OpenForms[FormName];
|
||||
if (form == null)
|
||||
return;
|
||||
|
||||
_dataGridView = (DataGridView)FindControlRecursive(form, ListViewName);
|
||||
|
||||
_dataGridView.Invoke(new Action(() =>
|
||||
{
|
||||
_dataGridView.Rows.Clear();
|
||||
DataGridViewTextBoxColumn logInfo = new DataGridViewTextBoxColumn();
|
||||
logInfo.Name = "Log Info";
|
||||
logInfo.DataPropertyName = "Log Info";
|
||||
logInfo.HeaderText = "Log Info";
|
||||
_dataGridView.Columns.Add(logInfo);
|
||||
_dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
|
||||
//设置自动换行
|
||||
_dataGridView.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
|
||||
//设置自动调整高度
|
||||
_dataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
|
||||
_dataGridView.RowHeadersVisible = false;
|
||||
_dataGridView.ColumnHeadersVisible = false;
|
||||
_dataGridView.AllowDrop = false;
|
||||
_dataGridView.AllowUserToAddRows = false;
|
||||
_dataGridView.AllowUserToDeleteRows = false;
|
||||
_dataGridView.AllowUserToOrderColumns = false;
|
||||
_dataGridView.AllowUserToResizeColumns = false;
|
||||
_dataGridView.AllowUserToResizeRows = false;
|
||||
_dataGridView.MultiSelect = false;
|
||||
_dataGridView.ReadOnly = true;
|
||||
_dataGridView.CellBorderStyle = DataGridViewCellBorderStyle.None;
|
||||
if (_dataGridView == null)
|
||||
return;
|
||||
|
||||
form.FormClosing += (s, e) => _dataGridView = null;
|
||||
}));
|
||||
}
|
||||
if (!_dataGridView.IsHandleCreated)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_dataGridView.IsDisposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//显示日志
|
||||
string str = String.Format("{0} {1}", loggingEvent.TimeStamp.ToString("yyyy/MM/dd HH:mm:ss.fff"), RenderLoggingEvent(loggingEvent));
|
||||
string level = loggingEvent.Level.ToString();
|
||||
|
||||
_dataGridView.BeginInvoke((MethodInvoker)delegate
|
||||
{
|
||||
//日志行数控制
|
||||
if (_dataGridView.Rows.Count > 500)
|
||||
{
|
||||
//大于200行清空显示
|
||||
_dataGridView.Rows.Clear();
|
||||
}
|
||||
DataGridViewRow row = new DataGridViewRow();
|
||||
|
||||
DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
|
||||
cell.Value = str;
|
||||
//根据等级控制显示颜色
|
||||
switch (level)
|
||||
{
|
||||
case "DEBUG":
|
||||
cell.Style.ForeColor = Color.Blue; //DarkBlue;
|
||||
break;
|
||||
case "INFO":
|
||||
cell.Style.ForeColor = Color.Black; //ForestGreen;
|
||||
break;
|
||||
case "WARN":
|
||||
cell.Style.ForeColor = Color.Orange;
|
||||
break;
|
||||
case "ERROR":
|
||||
cell.Style.ForeColor = Color.MediumVioletRed;
|
||||
break;
|
||||
case "FATAL":
|
||||
cell.Style.ForeColor = Color.Red;
|
||||
break;
|
||||
}
|
||||
row.Cells.Add(cell);
|
||||
//控制每次显示最新行
|
||||
_dataGridView.Rows.Add(row);
|
||||
_dataGridView.FirstDisplayedScrollingRowIndex = _dataGridView.Rows.Count - 1;
|
||||
_dataGridView.ClearSelection();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
54
精工涂胶检测项目/CVE.LogViewer/Helper.cs
Normal file
54
精工涂胶检测项目/CVE.LogViewer/Helper.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Xml;
|
||||
|
||||
namespace CVE.LogViewer
|
||||
{
|
||||
public class Helper
|
||||
{
|
||||
private const string FileName = "LogConfig.xml";
|
||||
public Stream GetXMLStream(string ConfigDir)
|
||||
{
|
||||
if (!System.IO.Directory.Exists(ConfigDir))
|
||||
{
|
||||
System.IO.Directory.CreateDirectory(ConfigDir);//不存在就创建目录
|
||||
}
|
||||
|
||||
string ConfigPath = ConfigDir + "\\" + FileName;
|
||||
//判断本地是否存在配置文件
|
||||
if (File.Exists(ConfigPath))
|
||||
{
|
||||
//导入本地配置文件
|
||||
Stream stream = new FileStream(ConfigPath, FileMode.Open, FileAccess.Read);
|
||||
return stream;
|
||||
}
|
||||
else
|
||||
{
|
||||
//载入程序集中配置文件
|
||||
var assembly = Assembly.GetExecutingAssembly();
|
||||
var stream = assembly.GetManifestResourceStream(this.GetType(), FileName);
|
||||
try
|
||||
{
|
||||
if (stream == null)
|
||||
{
|
||||
throw new FileNotFoundException("Could not find embedded mappings resource file.", FileName);
|
||||
}
|
||||
else
|
||||
{
|
||||
//将默认配置文件保存至指定目录
|
||||
XmlDocument doc = new XmlDocument();
|
||||
doc.Load(stream);
|
||||
doc.Save(ConfigPath);
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
42
精工涂胶检测项目/CVE.LogViewer/LogConfig.xml
Normal file
42
精工涂胶检测项目/CVE.LogViewer/LogConfig.xml
Normal file
@@ -0,0 +1,42 @@
|
||||
<?xml version="1.0"?>
|
||||
<configuration>
|
||||
<configSections>
|
||||
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
|
||||
</configSections>
|
||||
<log4net>
|
||||
<root>
|
||||
<level value="ALL"/>
|
||||
<!---日志级别 从低到高 ALL DEBUG INFO WARN ERROR FATAL None-->
|
||||
<appender-ref ref="RollingLogFileAppender"/>
|
||||
<appender-ref ref="DataGridViewAppender" />
|
||||
</root>
|
||||
<!-- name属性指定其名称,type则是log4net.Appender命名空间的一个类的名称,意思是指定使用哪种介质-->
|
||||
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
|
||||
<file value="Log\\"/>
|
||||
<!-- 输出到什么目录-->
|
||||
<appendToFile value="true"/>
|
||||
<!-- 是否到附加到文件中-->
|
||||
<rollingStyle value="Date"/>
|
||||
<!-- 文件创建方式,以日期的方式记录-->
|
||||
<datePattern value="'Log_'yyyy-MM-dd".txt""/>
|
||||
<!-- 文件格式-->
|
||||
<staticLogFileName value="false"/>
|
||||
<!--否采用静态文件名,文件名是否唯一-->
|
||||
<layout type="log4net.Layout.PatternLayout">
|
||||
<!---日志内容布局-->
|
||||
<param name="ConversionPattern" value="%date %-5level %message%newline"/>
|
||||
</layout>
|
||||
</appender>
|
||||
<!---listView日志显示-->
|
||||
<appender name="DataGridViewAppender" type="CVE.LogViewer.DataGridViewAppender, CVE.LogViewer">
|
||||
<formName value="FrmMain"/>
|
||||
<listViewName value="dgv_Log"/>
|
||||
<layout type="log4net.Layout.PatternLayout">
|
||||
<conversionPattern value="- %message" />
|
||||
</layout>
|
||||
</appender>
|
||||
</log4net>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
|
||||
</startup>
|
||||
</configuration>
|
||||
36
精工涂胶检测项目/CVE.LogViewer/Properties/AssemblyInfo.cs
Normal file
36
精工涂胶检测项目/CVE.LogViewer/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// 有关程序集的一般信息由以下
|
||||
// 控制。更改这些特性值可修改
|
||||
// 与程序集关联的信息。
|
||||
[assembly: AssemblyTitle("CVE.LogViewer")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("CVE.LogViewer")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2022")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// 将 ComVisible 设置为 false 会使此程序集中的类型
|
||||
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
|
||||
//请将此类型的 ComVisible 特性设置为 true。
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
||||
[assembly: Guid("766cf29f-3629-470a-aec7-d2aead685484")]
|
||||
|
||||
// 程序集的版本信息由下列四个值组成:
|
||||
//
|
||||
// 主版本
|
||||
// 次版本
|
||||
// 生成号
|
||||
// 修订号
|
||||
//
|
||||
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
|
||||
//通过使用 "*",如下所示:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
32444
精工涂胶检测项目/CVE.LogViewer/lib/log4net.xml
Normal file
32444
精工涂胶检测项目/CVE.LogViewer/lib/log4net.xml
Normal file
File diff suppressed because it is too large
Load Diff
4
精工涂胶检测项目/CVE.LogViewer/packages.config
Normal file
4
精工涂胶检测项目/CVE.LogViewer/packages.config
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="log4net" version="2.0.14" targetFramework="net471" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user