add log
This commit is contained in:
@@ -56,8 +56,18 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="FieldAttribute.cs" />
|
||||||
|
<Compile Include="FunctionAttribute.cs" />
|
||||||
|
<Compile Include="ILogService.cs" />
|
||||||
|
<Compile Include="IUserService.cs" />
|
||||||
|
<Compile Include="LogFilter.cs" />
|
||||||
|
<Compile Include="LogInterceptor.cs" />
|
||||||
|
<Compile Include="LogModel.cs" />
|
||||||
|
<Compile Include="LogService.cs" />
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="UserModel.cs" />
|
||||||
|
<Compile Include="UserService.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="App.config" />
|
<None Include="App.config" />
|
||||||
|
|||||||
9
src/AutofacWihtAOP/AutofacWihtAOP/FieldAttribute.cs
Normal file
9
src/AutofacWihtAOP/AutofacWihtAOP/FieldAttribute.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace AutofacWihtAOP
|
||||||
|
{
|
||||||
|
public class FieldAttribute : Attribute
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
9
src/AutofacWihtAOP/AutofacWihtAOP/FunctionAttribute.cs
Normal file
9
src/AutofacWihtAOP/AutofacWihtAOP/FunctionAttribute.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace AutofacWihtAOP
|
||||||
|
{
|
||||||
|
public class FunctionAttribute : Attribute
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
8
src/AutofacWihtAOP/AutofacWihtAOP/ILogService.cs
Normal file
8
src/AutofacWihtAOP/AutofacWihtAOP/ILogService.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
namespace AutofacWihtAOP
|
||||||
|
{
|
||||||
|
public interface ILogService
|
||||||
|
{
|
||||||
|
LogModel GetLastLog(LogFilter filter);
|
||||||
|
void AddLog(LogModel model);
|
||||||
|
}
|
||||||
|
}
|
||||||
7
src/AutofacWihtAOP/AutofacWihtAOP/IUserService.cs
Normal file
7
src/AutofacWihtAOP/AutofacWihtAOP/IUserService.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
namespace AutofacWihtAOP
|
||||||
|
{
|
||||||
|
public interface IUserService
|
||||||
|
{
|
||||||
|
void ModifyUserInfo(UserModel model);
|
||||||
|
}
|
||||||
|
}
|
||||||
9
src/AutofacWihtAOP/AutofacWihtAOP/LogFilter.cs
Normal file
9
src/AutofacWihtAOP/AutofacWihtAOP/LogFilter.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace AutofacWihtAOP
|
||||||
|
{
|
||||||
|
public class LogFilter
|
||||||
|
{
|
||||||
|
public string FunctionName { get; set; }
|
||||||
|
public string FieldName { get; set; }
|
||||||
|
public string UserCode { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
114
src/AutofacWihtAOP/AutofacWihtAOP/LogInterceptor.cs
Normal file
114
src/AutofacWihtAOP/AutofacWihtAOP/LogInterceptor.cs
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using Castle.DynamicProxy;
|
||||||
|
|
||||||
|
namespace AutofacWihtAOP
|
||||||
|
{
|
||||||
|
public class LogInterceptor : IInterceptor
|
||||||
|
{
|
||||||
|
private ILogService _logService;
|
||||||
|
|
||||||
|
public LogInterceptor(ILogService logservice)
|
||||||
|
{
|
||||||
|
_logService = logservice;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Intercept(IInvocation invocation)
|
||||||
|
{
|
||||||
|
var models = invocation.Arguments.Where(Ext.IsAttributeType<FunctionAttribute>);
|
||||||
|
|
||||||
|
var selectMany = models.SelectMany(x=> x.GetType().GetPropertiesBy<FieldAttribute>(),
|
||||||
|
(model,prop)=> new
|
||||||
|
{
|
||||||
|
CurrentValue = prop.GetValue(model),
|
||||||
|
FieldName = prop.GetAttributeValue((FieldAttribute z) => z.Name),
|
||||||
|
functionName = model.GetType()
|
||||||
|
.GetAttributeValue((FunctionAttribute attr) => attr.Name)
|
||||||
|
});
|
||||||
|
|
||||||
|
foreach (var prop in selectMany)
|
||||||
|
{
|
||||||
|
|
||||||
|
var lastLog = _logService.GetLastLog(new LogFilter()
|
||||||
|
{
|
||||||
|
FieldName = prop.FieldName,
|
||||||
|
FunctionName = prop.functionName
|
||||||
|
});
|
||||||
|
|
||||||
|
var logModel = new LogModel()
|
||||||
|
{
|
||||||
|
UserCode = "Dnaiel",
|
||||||
|
FunctionName = prop.functionName,
|
||||||
|
FieldName = prop.FieldName,
|
||||||
|
NewValue = prop.CurrentValue?.ToString()
|
||||||
|
};
|
||||||
|
|
||||||
|
if (lastLog != null)
|
||||||
|
logModel.OldValue = lastLog.NewValue;
|
||||||
|
else
|
||||||
|
logModel.OldValue = string.Empty;
|
||||||
|
|
||||||
|
_logService.AddLog(logModel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Ext
|
||||||
|
{
|
||||||
|
public static IEnumerable<PropertyInfo> GetPropertiesBy<T>
|
||||||
|
(this Type type,bool inherit = true)
|
||||||
|
where T : Attribute
|
||||||
|
{
|
||||||
|
if (type == null)
|
||||||
|
throw new Exception("type can't be null");
|
||||||
|
|
||||||
|
|
||||||
|
return type.GetProperties()
|
||||||
|
.Where(x => x.GetCustomAttribute<T>(inherit) != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool IsAttributeType<TAttr>(this object obj)
|
||||||
|
where TAttr : Attribute
|
||||||
|
{
|
||||||
|
return IsAttributeType<TAttr>(obj, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool IsAttributeType<TAttr>(this object obj,bool inherit)
|
||||||
|
where TAttr : Attribute
|
||||||
|
{
|
||||||
|
return obj.GetType()
|
||||||
|
.GetCustomAttribute<TAttr>(inherit) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TRtn GetAttributeValue<TAttr, TRtn>(this PropertyInfo prop,
|
||||||
|
Func<TAttr,TRtn> selector)
|
||||||
|
where TAttr : Attribute
|
||||||
|
{
|
||||||
|
TRtn result = default(TRtn);
|
||||||
|
|
||||||
|
var attr = prop.GetCustomAttribute<TAttr>();
|
||||||
|
|
||||||
|
if (selector == null || attr == null)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
return selector(attr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TRtn GetAttributeValue<TAttr, TRtn>(this Type prop,
|
||||||
|
Func<TAttr, TRtn> selector)
|
||||||
|
where TAttr : Attribute
|
||||||
|
{
|
||||||
|
TRtn result = default(TRtn);
|
||||||
|
|
||||||
|
var attr = prop.GetCustomAttribute<TAttr>();
|
||||||
|
|
||||||
|
if (selector == null || attr == null)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
return selector(attr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
19
src/AutofacWihtAOP/AutofacWihtAOP/LogModel.cs
Normal file
19
src/AutofacWihtAOP/AutofacWihtAOP/LogModel.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace AutofacWihtAOP
|
||||||
|
{
|
||||||
|
public class LogModel
|
||||||
|
{
|
||||||
|
public DateTime CreateDate { get; set; } = DateTime.Now;
|
||||||
|
|
||||||
|
public string OldValue { get; set; }
|
||||||
|
|
||||||
|
public string NewValue { get; set; }
|
||||||
|
|
||||||
|
public string UserCode { get; set; }
|
||||||
|
|
||||||
|
public string FunctionName { get; set; }
|
||||||
|
|
||||||
|
public string FieldName { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
23
src/AutofacWihtAOP/AutofacWihtAOP/LogService.cs
Normal file
23
src/AutofacWihtAOP/AutofacWihtAOP/LogService.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace AutofacWihtAOP
|
||||||
|
{
|
||||||
|
public class LogService : ILogService
|
||||||
|
{
|
||||||
|
List<LogModel> _list = new List<LogModel>();
|
||||||
|
|
||||||
|
public LogModel GetLastLog(LogFilter filter)
|
||||||
|
{
|
||||||
|
return _list.OrderBy(x => x.CreateDate)
|
||||||
|
.FirstOrDefault(x=>
|
||||||
|
x.FieldName == filter.FieldName &&
|
||||||
|
x.FunctionName == filter.FunctionName );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddLog(LogModel model)
|
||||||
|
{
|
||||||
|
_list.Add(model);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,8 @@ using Autofac;
|
|||||||
using Autofac.Builder;
|
using Autofac.Builder;
|
||||||
using Autofac.Extras.DynamicProxy;
|
using Autofac.Extras.DynamicProxy;
|
||||||
using Castle.DynamicProxy;
|
using Castle.DynamicProxy;
|
||||||
|
using static System.Convert;
|
||||||
|
|
||||||
namespace AutofacWihtAOP
|
namespace AutofacWihtAOP
|
||||||
{
|
{
|
||||||
public interface ITimeService
|
public interface ITimeService
|
||||||
@@ -69,6 +71,7 @@ namespace AutofacWihtAOP
|
|||||||
|
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -76,9 +79,17 @@ namespace AutofacWihtAOP
|
|||||||
|
|
||||||
IPerson person = container.Resolve<IPerson>();
|
IPerson person = container.Resolve<IPerson>();
|
||||||
|
|
||||||
Console.WriteLine(person.SaySomething());
|
//Console.WriteLine(person.SaySomething());
|
||||||
Thread.Sleep(5000);
|
//Thread.Sleep(5000);
|
||||||
Console.WriteLine(person.SaySomething());
|
//Console.WriteLine(person.SaySomething());
|
||||||
|
|
||||||
|
IUserService personService = container.Resolve<IUserService>();
|
||||||
|
|
||||||
|
personService.ModifyUserInfo(new UserModel()
|
||||||
|
{
|
||||||
|
Birthday = DateTime.Now,
|
||||||
|
Phone = "0911181212"
|
||||||
|
});
|
||||||
|
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
}
|
}
|
||||||
@@ -88,11 +99,20 @@ namespace AutofacWihtAOP
|
|||||||
var builder = new ContainerBuilder();
|
var builder = new ContainerBuilder();
|
||||||
|
|
||||||
builder.RegisterType<TimeInterceptor>(); //註冊攔截器
|
builder.RegisterType<TimeInterceptor>(); //註冊攔截器
|
||||||
|
builder.RegisterType<LogInterceptor>(); //註冊攔截器
|
||||||
|
|
||||||
|
builder.RegisterType<LogService>()
|
||||||
|
.As<ILogService>();
|
||||||
|
|
||||||
builder.RegisterType<Person>()
|
builder.RegisterType<Person>()
|
||||||
.As<IPerson>()
|
.As<IPerson>()
|
||||||
.EnableInterfaceInterceptors();
|
.EnableInterfaceInterceptors();
|
||||||
|
|
||||||
|
builder.RegisterType<UserService>()
|
||||||
|
.As<IUserService>()
|
||||||
|
.EnableInterfaceInterceptors();
|
||||||
|
|
||||||
|
|
||||||
//註冊時間Service
|
//註冊時間Service
|
||||||
builder.RegisterType<TimeService>().As<ITimeService>();
|
builder.RegisterType<TimeService>().As<ITimeService>();
|
||||||
|
|
||||||
|
|||||||
13
src/AutofacWihtAOP/AutofacWihtAOP/UserModel.cs
Normal file
13
src/AutofacWihtAOP/AutofacWihtAOP/UserModel.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace AutofacWihtAOP
|
||||||
|
{
|
||||||
|
[Function(Name = "Login")]
|
||||||
|
public class UserModel
|
||||||
|
{
|
||||||
|
[Field(Name = "Phone")]
|
||||||
|
public string Phone { get; set; }
|
||||||
|
[Field(Name = "Birthday")]
|
||||||
|
public DateTime Birthday { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/AutofacWihtAOP/AutofacWihtAOP/UserService.cs
Normal file
13
src/AutofacWihtAOP/AutofacWihtAOP/UserService.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using Autofac.Extras.DynamicProxy;
|
||||||
|
|
||||||
|
namespace AutofacWihtAOP
|
||||||
|
{
|
||||||
|
[Intercept(typeof(LogInterceptor))]
|
||||||
|
public class UserService:IUserService
|
||||||
|
{
|
||||||
|
public void ModifyUserInfo(UserModel model)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user