增加系统异常日志记录功能

This commit is contained in:
Argo-Lenovo 2016-11-10 16:39:07 +08:00
parent d7ab014898
commit 208cb9bede
11 changed files with 330 additions and 200 deletions

View File

@ -0,0 +1,33 @@
using Bootstrap.DataAccess;
using Longbow.Caching;
using Longbow.ExceptionManagement;
using Longbow.ExceptionManagement.Configuration;
using System;
using System.Collections.Specialized;
using System.Web;
namespace Bootstrap.Admin
{
/// <summary>
///
/// </summary>
public class DBPublisher : IExceptionPublisher
{
/// <summary>
///
/// </summary>
/// <param name="ex"></param>
/// <param name="additionalInfo"></param>
/// <param name="publisherElement"></param>
public void Publish(Exception ex, NameValueCollection additionalInfo, ExceptionPublisherElement publisherElement)
{
if (publisherElement.Mode == PublisherMode.Off) return;
HttpContext context = HttpContext.Current;
additionalInfo["ErrorPage"] = context.Request.AppRelativeCurrentExecutionFilePath;
if (context.User != null) additionalInfo["UserId"] = context.User.Identity.Name;
additionalInfo["UserIp"] = context.Request.UserHostAddress;
ExceptionHelper.Log(ex, additionalInfo);
CacheManager.Clear();
}
}
}

View File

@ -43,6 +43,7 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Longbow.ExceptionManagement, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c20f2177a7066899, processorArchitecture=MSIL" />
<Reference Include="Longbow.V4, Version=4.0.0.0, Culture=neutral, PublicKeyToken=c20f2177a7066899, processorArchitecture=MSIL" /> <Reference Include="Longbow.V4, Version=4.0.0.0, Culture=neutral, PublicKeyToken=c20f2177a7066899, processorArchitecture=MSIL" />
<Reference Include="Longbow.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=c20f2177a7066899, processorArchitecture=MSIL" /> <Reference Include="Longbow.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=c20f2177a7066899, processorArchitecture=MSIL" />
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
@ -205,6 +206,7 @@
<Link>bldver.cs</Link> <Link>bldver.cs</Link>
</Compile> </Compile>
<Compile Include="App_Start\BAAuthorizeAttribute.cs" /> <Compile Include="App_Start\BAAuthorizeAttribute.cs" />
<Compile Include="App_Start\DBPublisher.cs" />
<Compile Include="App_Start\FilterConfig.cs" /> <Compile Include="App_Start\FilterConfig.cs" />
<Compile Include="App_Start\RouteConfig.cs" /> <Compile Include="App_Start\RouteConfig.cs" />
<Compile Include="App_Start\WebApiConfig.cs" /> <Compile Include="App_Start\WebApiConfig.cs" />

View File

@ -49,6 +49,7 @@ namespace Bootstrap.Admin.Controllers
{ {
//UNDONE: 本方法有严重安全漏洞,发布前需要修正 //UNDONE: 本方法有严重安全漏洞,发布前需要修正
var model = new LoginModel(); var model = new LoginModel();
if (string.IsNullOrEmpty(userName)) return View(model);
model.UserName = userName; model.UserName = userName;
if (LgbPrincipal.IsAdmin(userName) || UserHelper.Authenticate(userName, password)) if (LgbPrincipal.IsAdmin(userName) || UserHelper.Authenticate(userName, password))
{ {

View File

@ -21,6 +21,8 @@
</li> </li>
</ul> </ul>
} }
@if (!Model.ShowMenu)
{
<!-- notification start --> <!-- notification start -->
<ul class="notify-row"> <ul class="notify-row">
<!-- settings start --> <!-- settings start -->
@ -216,6 +218,7 @@
<!-- notification dropdown end --> <!-- notification dropdown end -->
</ul> </ul>
<!-- notification end --> <!-- notification end -->
}
<!--search & user info start--> <!--search & user info start-->
<ul class="nav pull-right top-menu"> <ul class="nav pull-right top-menu">
<li> <li>
@ -231,9 +234,9 @@
</a> </a>
<ul class="dropdown-menu logout"> <ul class="dropdown-menu logout">
<div class="arrow-up"></div> <div class="arrow-up"></div>
<li><a href="#"><i class=" fa fa-suitcase"></i>个人中心</a></li> <li><a href="~/Admin/Infos"><i class=" fa fa-suitcase"></i>个人中心</a></li>
<li><a href="~/Admin"><i class="fa fa-cog"></i>设置</a></li> <li><a href="~/Admin/Index"><i class="fa fa-cog"></i>设置</a></li>
<li><a href="#"><i class="fa fa-bell-o"></i>通知<span class="badge bg-success">5</span></a></li> <li><a href="~/Admin/News"><i class="fa fa-bell-o"></i>通知<span class="badge bg-success">5</span></a></li>
<li><a href="~/Home/Logout"><i class="fa fa-key"></i>注销</a></li> <li><a href="~/Home/Logout"><i class="fa fa-key"></i>注销</a></li>
</ul> </ul>
</li> </li>

View File

@ -5,6 +5,7 @@
--> -->
<configuration> <configuration>
<configSections> <configSections>
<section name="exceptionManagement" type="Longbow.ExceptionManagement.Configuration.ExceptionManagerSectionHandler, Longbow.ExceptionManagement" />
<section name="cacheManager" type="Longbow.Caching.Configuration.CacheSection, Longbow.V4"/> <section name="cacheManager" type="Longbow.Caching.Configuration.CacheSection, Longbow.V4"/>
<section name="cacheManagerList" type="Longbow.Caching.Configuration.CacheListSection, Longbow.V4"/> <section name="cacheManagerList" type="Longbow.Caching.Configuration.CacheListSection, Longbow.V4"/>
</configSections> </configSections>
@ -47,6 +48,11 @@
<add key="bd" url="http://localhost:53233/CacheList.axd?cacheKey={0}" desc="系统缓存模块"/> <add key="bd" url="http://localhost:53233/CacheList.axd?cacheKey={0}" desc="系统缓存模块"/>
</cacheManagerList> </cacheManagerList>
<exceptionManagement>
<publisher name="default" mode="On" exceptionFormat="File"/>
<publisher name="db" mode="On" assembly="Bootstrap.Admin" type="Bootstrap.Admin.DBPublisher"/>
</exceptionManagement>
<system.web> <system.web>
<compilation debug="true" targetFramework="4.5"/> <compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/> <httpRuntime targetFramework="4.5"/>

View File

@ -60,6 +60,7 @@
<Compile Include="DBAccessManager.cs" /> <Compile Include="DBAccessManager.cs" />
<Compile Include="Dict.cs" /> <Compile Include="Dict.cs" />
<Compile Include="DictHelper.cs" /> <Compile Include="DictHelper.cs" />
<Compile Include="ExceptionHelper.cs" />
<Compile Include="Group.cs" /> <Compile Include="Group.cs" />
<Compile Include="GroupHelper.cs" /> <Compile Include="GroupHelper.cs" />
<Compile Include="Log.cs" /> <Compile Include="Log.cs" />

View File

@ -0,0 +1,33 @@
using Longbow.Data;
using System;
using System.Collections.Specialized;
using System.Data;
using System.Data.Common;
namespace Bootstrap.DataAccess
{
public static class ExceptionHelper
{
public static void Log(Exception ex, NameValueCollection additionalInfo)
{
try
{
var sql = "insert into Exceptions (AppDomainName, ErrorPage, UserID, UserIp, Message, StackTrace, LogTime) values (@AppDomainName, @ErrorPage, @UserID, @UserIp, @Message, @StackTrace, GetDate())";
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@AppDomainName", AppDomain.CurrentDomain.FriendlyName, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ErrorPage", additionalInfo["ErrorPage"], ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", DBAccess.ToDBValue(additionalInfo["UserId"]), ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserIp", additionalInfo["UserIp"], ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Message", ex.Message, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@StackTrace", DBAccess.ToDBValue(ex.StackTrace), ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
}
catch
{
throw new Exception("Excetion Log Error");
}
}
}
}

View File

@ -55,6 +55,7 @@
</Choose> </Choose>
<ItemGroup> <ItemGroup>
<Compile Include="DictHelperTests.cs" /> <Compile Include="DictHelperTests.cs" />
<Compile Include="ExceptionHelperTests.cs" />
<Compile Include="RoleHelperTests.cs" /> <Compile Include="RoleHelperTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="GroupHelperTests.cs" /> <Compile Include="GroupHelperTests.cs" />

View File

@ -0,0 +1,20 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Specialized;
namespace Bootstrap.DataAccess.Tests
{
[TestClass()]
public class ExceptionHelperTests
{
[TestMethod()]
public void LogTest()
{
var ex = new Exception("Just for Test");
var nv = new NameValueCollection();
nv.Add("ErrorPage", "UnitTest_Page");
nv.Add("UserIp", "::1");
ExceptionHelper.Log(ex, nv);
}
}
}

View File

@ -6,7 +6,7 @@ SET IDENTITY_INSERT [dbo].[Users] ON
insert into Users (ID, UserName, Password, PassSalt, DisplayName, RegisterTime, ApprovedTime, [Description]) values (1, 'Admin', 'Es7WVgNsJuELwWK8daCqufUBknCsSC0IYDphQZAiGOo=', 'W5vpBEOYRGHkQXatN0t+ECM/U8cHDuEgrq56+zZBk4J481xH', 'Administrator', GetDate(), GetDate(), N'系统默认创建') insert into Users (ID, UserName, Password, PassSalt, DisplayName, RegisterTime, ApprovedTime, [Description]) values (1, 'Admin', 'Es7WVgNsJuELwWK8daCqufUBknCsSC0IYDphQZAiGOo=', 'W5vpBEOYRGHkQXatN0t+ECM/U8cHDuEgrq56+zZBk4J481xH', 'Administrator', GetDate(), GetDate(), N'系统默认创建')
SET IDENTITY_INSERT [dbo].[Users] OFF SET IDENTITY_INSERT [dbo].[Users] OFF
DELETE From Dicts where ID < 10 DELETE From Dicts where ID < 12
SET IDENTITY_INSERT [dbo].[Dicts] ON SET IDENTITY_INSERT [dbo].[Dicts] ON
INSERT [dbo].[Dicts] ([ID], [Category], [Name], [Code], [Define]) VALUES (1, N'菜单', N'系统菜单', N'0', 0) INSERT [dbo].[Dicts] ([ID], [Category], [Name], [Code], [Define]) VALUES (1, N'菜单', N'系统菜单', N'0', 0)
INSERT [dbo].[Dicts] ([ID], [Category], [Name], [Code], [Define]) VALUES (2, N'菜单', N'外部菜单', N'1', 0) INSERT [dbo].[Dicts] ([ID], [Category], [Name], [Code], [Define]) VALUES (2, N'菜单', N'外部菜单', N'1', 0)
@ -77,4 +77,6 @@ INSERT [dbo].[NavigationRole] ([ID], [NavigationID], [RoleID]) VALUES (12, 11, 2
INSERT [dbo].[NavigationRole] ([ID], [NavigationID], [RoleID]) VALUES (13, 12, 1) INSERT [dbo].[NavigationRole] ([ID], [NavigationID], [RoleID]) VALUES (13, 12, 1)
INSERT [dbo].[NavigationRole] ([ID], [NavigationID], [RoleID]) VALUES (14, 13, 2) INSERT [dbo].[NavigationRole] ([ID], [NavigationID], [RoleID]) VALUES (14, 13, 2)
INSERT [dbo].[NavigationRole] ([ID], [NavigationID], [RoleID]) VALUES (15, 14, 2) INSERT [dbo].[NavigationRole] ([ID], [NavigationID], [RoleID]) VALUES (15, 14, 2)
INSERT [dbo].[NavigationRole] ([ID], [NavigationID], [RoleID]) VALUES (16, 1, 2)
INSERT [dbo].[NavigationRole] ([ID], [NavigationID], [RoleID]) VALUES (17, 10, 2)
SET IDENTITY_INSERT [dbo].[NavigationRole] OFF SET IDENTITY_INSERT [dbo].[NavigationRole] OFF

View File

@ -136,7 +136,7 @@ GO
SET QUOTED_IDENTIFIER ON SET QUOTED_IDENTIFIER ON
GO GO
CREATE TABLE [dbo].[Notifications]( CREATE TABLE [dbo].[Notifications](
[ID] [int] NOT NULL, [ID] [int] IDENTITY(1,1) NOT NULL,
[Category] [nvarchar](50) NOT NULL, [Category] [nvarchar](50) NOT NULL,
[Title] [nvarchar](50) NOT NULL, [Title] [nvarchar](50) NOT NULL,
[Content] [nvarchar](50) NOT NULL, [Content] [nvarchar](50) NOT NULL,
@ -144,7 +144,11 @@ CREATE TABLE [dbo].[Notifications](
[ProcessTime] [datetime] NULL, [ProcessTime] [datetime] NULL,
[ProcessBy] [nvarchar](50) NULL, [ProcessBy] [nvarchar](50) NULL,
[ProcessResult] [nvarchar](50) NULL, [ProcessResult] [nvarchar](50) NULL,
[Status] [nvarchar](50) NOT NULL [Status] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Notifications] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] ) ON [PRIMARY]
GO GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'0 标示未处理 1 标示已处理' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Notifications', @level2type=N'COLUMN',@level2name=N'Status' EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'0 标示未处理 1 标示已处理' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Notifications', @level2type=N'COLUMN',@level2name=N'Status'
@ -235,7 +239,31 @@ EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'部门名称'
GO GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'描述' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Groups', @level2type=N'COLUMN',@level2name=N'Description' EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'描述' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Groups', @level2type=N'COLUMN',@level2name=N'Description'
GO GO
/****** Object: Table [dbo].[Dicts] Script Date: 11/10/2016 11:32:54 ******/ /****** Object: Table [dbo].[Exceptions] Script Date: 11/10/2016 16:30:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Exceptions](
[ID] [int] IDENTITY(1,1) NOT NULL,
[AppDomainName] [varchar](50) NOT NULL,
[ErrorPage] [varchar](50) NOT NULL,
[UserID] [varchar](50) NULL,
[UserIp] [varchar](15) NOT NULL,
[Message] [nvarchar](max) NOT NULL,
[StackTrace] [nvarchar](max) NULL,
[LogTime] [datetime] NOT NULL,
CONSTRAINT [PK_Exceptions] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Dicts] Script Date: 11/10/2016 16:30:56 ******/
SET ANSI_NULLS ON SET ANSI_NULLS ON
GO GO
SET QUOTED_IDENTIFIER ON SET QUOTED_IDENTIFIER ON