forked from SharpRepository/SharpRepository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepositoryLoggingAttribute.cs
More file actions
178 lines (141 loc) · 6.73 KB
/
RepositoryLoggingAttribute.cs
File metadata and controls
178 lines (141 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using Common.Logging;
using SharpRepository.Repository;
using SharpRepository.Repository.Aspects;
namespace SharpRepository.Logging
{
public class RepositoryLoggingAttribute : RepositoryActionBaseAttribute
{
private readonly ILog _logger;
private LogLevel _logLevel = LogLevel.Debug;
public RepositoryLoggingAttribute()
{
_logger = LogManager.GetLogger("SharpRepository");
}
public LogLevel LogLevel
{
get { return _logLevel; }
set { _logLevel = value; }
}
private void Log(string message)
{
switch (_logLevel)
{
case LogLevel.Error:
_logger.Error(message);
break;
case LogLevel.Info:
_logger.Info(message);
break;
case LogLevel.Trace:
_logger.Trace(message);
break;
default:
_logger.Debug(message);
break;
}
}
public override void OnInitialized<T, TKey>(RepositoryActionContext<T, TKey> context)
{
Log(String.Format("Initialized IRepository<{0}, {1}>", typeof(T).Name, typeof(TKey).Name));
}
public override bool OnAddExecuting<T, TKey>(T entity, RepositoryActionContext<T, TKey> context)
{
Log(String.Format("Adding {0} entity", typeof(T).Name));
Log(String.Format(" {0}", entity.ToString()));
return true;
}
public override void OnAddExecuted<T, TKey>(T entity, RepositoryActionContext<T, TKey> context)
{
Log(String.Format("Added {0} entity", typeof(T).Name));
Log(String.Format(" {0}", entity.ToString()));
}
public override bool OnUpdateExecuting<T, TKey>(T entity, RepositoryActionContext<T, TKey> context)
{
Log(String.Format("Updating {0} entity", typeof(T).Name));
Log(String.Format(" {0}", entity.ToString()));
return true;
}
public override void OnUpdateExecuted<T, TKey>(T entity, RepositoryActionContext<T, TKey> context)
{
Log(String.Format("Updated {0} entity", typeof(T).Name));
Log(String.Format(" {0}", entity.ToString()));
}
public override bool OnDeleteExecuting<T, TKey>(T entity, RepositoryActionContext<T, TKey> context)
{
Log(String.Format("Deleting {0} entity", typeof(T).Name));
Log(String.Format(" {0}", entity.ToString()));
return true;
}
public override void OnDeleteExecuted<T, TKey>(T entity, RepositoryActionContext<T, TKey> context)
{
Log(String.Format("Deleted {0} entity", typeof(T).Name));
Log(String.Format(" {0}", entity.ToString()));
}
public override bool OnSaveExecuting<T, TKey>(RepositoryActionContext<T, TKey> context)
{
Log(String.Format("Saving {0} entity", typeof(T).Name));
return true;
}
public override void OnSaveExecuted<T, TKey>(RepositoryActionContext<T, TKey> context)
{
Log(String.Format("Saved {0} entity", typeof(T).Name));
}
public override bool OnGetExecuting<T, TKey, TResult>(RepositoryGetContext<T, TKey, TResult> context)
{
var typeDisplay = RepositoryTypeDisplay(context.Repository);
Log(String.Format("{0} Executing Get: Id = {1}", typeDisplay, context.Id));
return true;
}
public override void OnGetExecuted<T, TKey, TResult>(RepositoryGetContext<T, TKey, TResult> context)
{
var typeDisplay = RepositoryTypeDisplay(context.Repository);
Log(String.Format("{0} Executed Get: Id = {1}", typeDisplay, context.Id));
Log(context.Repository.TraceInfo);
Log(String.Format("{0} Has Result: {1} Cache Used: {2}", typeDisplay, context.HasResult, context.Repository.CacheUsed));
}
public override bool OnGetAllExecuting<T, TKey, TResult>(RepositoryQueryMultipleContext<T, TKey, TResult> context)
{
var typeDisplay = RepositoryTypeDisplay(context.Repository);
Log(String.Format("{0} Executing GetAll", typeDisplay));
return true;
}
public override void OnGetAllExecuted<T, TKey, TResult>(RepositoryQueryMultipleContext<T, TKey, TResult> context)
{
var typeDisplay = RepositoryTypeDisplay(context.Repository);
Log(String.Format("{0} Executed GetAll", typeDisplay));
Log(context.Repository.TraceInfo);
Log(String.Format("{0} Results: {1} Cache Used: {2}", typeDisplay, context.NumberOfResults, context.Repository.CacheUsed));
}
public override bool OnFindExecuting<T, TKey, TResult>(RepositoryQuerySingleContext<T, TKey, TResult> context)
{
var typeDisplay = RepositoryTypeDisplay(context.Repository);
Log(String.Format("{0} Executing Find: {1}", typeDisplay, context.Specification.Predicate));
return true;
}
public override void OnFindExecuted<T, TKey, TResult>(RepositoryQuerySingleContext<T, TKey, TResult> context)
{
var typeDisplay = RepositoryTypeDisplay(context.Repository);
Log(String.Format("{0} Executed Find: {1}", typeDisplay, context.Specification.Predicate));
Log(context.Repository.TraceInfo);
Log(String.Format("{0} Results: {1} Cache Used: {2}", typeDisplay, context.NumberOfResults, context.Repository.CacheUsed));
}
public override bool OnFindAllExecuting<T, TKey, TResult>(RepositoryQueryMultipleContext<T, TKey, TResult> context)
{
var typeDisplay = RepositoryTypeDisplay(context.Repository);
Log(String.Format("{0} Executing FindAll: {1}", typeDisplay, context.Specification.Predicate));
return true;
}
public override void OnFindAllExecuted<T, TKey, TResult>(RepositoryQueryMultipleContext<T, TKey, TResult> context)
{
var typeDisplay = RepositoryTypeDisplay(context.Repository);
Log(String.Format("{0} Executed FindAll: {1}", typeDisplay, context.Specification.Predicate));
Log(context.Repository.TraceInfo);
Log(String.Format("{0} Results: {1} Cache Used: {2}", typeDisplay, context.NumberOfResults, context.Repository.CacheUsed));
}
private static string RepositoryTypeDisplay<T, TKey>(IRepository<T, TKey> repository) where T : class
{
return String.Format("[{0}<{1},{2}>]", repository.GetType().Name, typeof(T).Name, typeof(TKey).Name);
}
}
}