Skip to content

Commit f4d7e8d

Browse files
author
Jeff Treuting
committed
Enabled flag on base aspect attribute, logging level choice
1 parent bdbb0f6 commit f4d7e8d

4 files changed

Lines changed: 57 additions & 33 deletions

File tree

SharpRepository.Logging/RepositoryLoggingAttribute.cs

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,132 +8,153 @@ namespace SharpRepository.Logging
88
public class RepositoryLoggingAttribute : RepositoryActionBaseAttribute
99
{
1010
private readonly ILog _logger;
11+
private readonly LogLevel _logLevel = LogLevel.Debug;
1112

12-
public RepositoryLoggingAttribute()
13+
public RepositoryLoggingAttribute(LogLevel logLevel = LogLevel.Debug)
1314
{
1415
_logger = LogManager.GetLogger("SharpRepository");
16+
_logLevel = logLevel;
17+
}
18+
19+
private void Log(string message)
20+
{
21+
switch (_logLevel)
22+
{
23+
case LogLevel.Error:
24+
_logger.Error(message);
25+
break;
26+
case LogLevel.Info:
27+
_logger.Info(message);
28+
break;
29+
case LogLevel.Trace:
30+
_logger.Trace(message);
31+
break;
32+
default:
33+
_logger.Debug(message);
34+
break;
35+
}
1536
}
1637

1738
public override void OnInitialized<T, TKey>(RepositoryActionContext<T, TKey> context)
1839
{
19-
_logger.Debug(String.Format("Initialized IRepository<{0}, {1}>", typeof(T).Name, typeof(TKey).Name));
40+
Log(String.Format("Initialized IRepository<{0}, {1}>", typeof(T).Name, typeof(TKey).Name));
2041
}
2142

2243
public override bool OnAddExecuting<T, TKey>(T entity, RepositoryActionContext<T, TKey> context)
2344
{
24-
_logger.Debug(String.Format("Adding {0} entity", typeof(T).Name));
25-
_logger.Debug(String.Format(" {0}", entity.ToString()));
45+
Log(String.Format("Adding {0} entity", typeof(T).Name));
46+
Log(String.Format(" {0}", entity.ToString()));
2647
return true;
2748
}
2849

2950
public override void OnAddExecuted<T, TKey>(T entity, RepositoryActionContext<T, TKey> context)
3051
{
31-
_logger.Debug(String.Format("Added {0} entity", typeof(T).Name));
32-
_logger.Debug(String.Format(" {0}", entity.ToString()));
52+
Log(String.Format("Added {0} entity", typeof(T).Name));
53+
Log(String.Format(" {0}", entity.ToString()));
3354
}
3455

3556
public override bool OnUpdateExecuting<T, TKey>(T entity, RepositoryActionContext<T, TKey> context)
3657
{
37-
_logger.Debug(String.Format("Updating {0} entity", typeof(T).Name));
38-
_logger.Debug(String.Format(" {0}", entity.ToString()));
58+
Log(String.Format("Updating {0} entity", typeof(T).Name));
59+
Log(String.Format(" {0}", entity.ToString()));
3960

4061
return true;
4162
}
4263

4364
public override void OnUpdateExecuted<T, TKey>(T entity, RepositoryActionContext<T, TKey> context)
4465
{
45-
_logger.Debug(String.Format("Updated {0} entity", typeof(T).Name));
46-
_logger.Debug(String.Format(" {0}", entity.ToString()));
66+
Log(String.Format("Updated {0} entity", typeof(T).Name));
67+
Log(String.Format(" {0}", entity.ToString()));
4768
}
4869

4970
public override bool OnDeleteExecuting<T, TKey>(T entity, RepositoryActionContext<T, TKey> context)
5071
{
51-
_logger.Debug(String.Format("Deleting {0} entity", typeof(T).Name));
52-
_logger.Debug(String.Format(" {0}", entity.ToString()));
72+
Log(String.Format("Deleting {0} entity", typeof(T).Name));
73+
Log(String.Format(" {0}", entity.ToString()));
5374

5475
return true;
5576
}
5677

5778
public override void OnDeleteExecuted<T, TKey>(T entity, RepositoryActionContext<T, TKey> context)
5879
{
59-
_logger.Debug(String.Format("Deleted {0} entity", typeof(T).Name));
60-
_logger.Debug(String.Format(" {0}", entity.ToString()));
80+
Log(String.Format("Deleted {0} entity", typeof(T).Name));
81+
Log(String.Format(" {0}", entity.ToString()));
6182
}
6283

6384
public override bool OnSaveExecuting<T, TKey>(RepositoryActionContext<T, TKey> context)
6485
{
65-
_logger.Debug(String.Format("Saving {0} entity", typeof(T).Name));
86+
Log(String.Format("Saving {0} entity", typeof(T).Name));
6687

6788
return true;
6889
}
6990

7091
public override void OnSaveExecuted<T, TKey>(RepositoryActionContext<T, TKey> context)
7192
{
72-
_logger.Debug(String.Format("Saved {0} entity", typeof(T).Name));
93+
Log(String.Format("Saved {0} entity", typeof(T).Name));
7394
}
7495

7596
public override void OnGetExecuting<T, TKey, TResult>(RepositoryGetContext<T, TKey, TResult> context)
7697
{
7798
var typeDisplay = RepositoryTypeDisplay(context.Repository);
7899

79-
_logger.Debug(String.Format("{0} Executing Get: Id = {1}", typeDisplay, context.Id));
100+
Log(String.Format("{0} Executing Get: Id = {1}", typeDisplay, context.Id));
80101
}
81102

82103
public override void OnGetExecuted<T, TKey, TResult>(RepositoryGetContext<T, TKey, TResult> context)
83104
{
84105
var typeDisplay = RepositoryTypeDisplay(context.Repository);
85106

86-
_logger.Debug(String.Format("{0} Executed Get: Id = {1}", typeDisplay, context.Id));
87-
_logger.Debug(context.Repository.TraceInfo);
88-
_logger.Debug(String.Format("{0} Has Result: {1} Cache Used: {2}", typeDisplay, context.HasResult, context.Repository.CacheUsed));
107+
Log(String.Format("{0} Executed Get: Id = {1}", typeDisplay, context.Id));
108+
Log(context.Repository.TraceInfo);
109+
Log(String.Format("{0} Has Result: {1} Cache Used: {2}", typeDisplay, context.HasResult, context.Repository.CacheUsed));
89110
}
90111

91112
public override void OnGetAllExecuting<T, TKey, TResult>(RepositoryQueryMultipleContext<T, TKey, TResult> context)
92113
{
93114
var typeDisplay = RepositoryTypeDisplay(context.Repository);
94115

95-
_logger.Debug(String.Format("{0} Executing GetAll", typeDisplay));
116+
Log(String.Format("{0} Executing GetAll", typeDisplay));
96117
}
97118

98119
public override void OnGetAllExecuted<T, TKey, TResult>(RepositoryQueryMultipleContext<T, TKey, TResult> context)
99120
{
100121
var typeDisplay = RepositoryTypeDisplay(context.Repository);
101122

102-
_logger.Debug(String.Format("{0} Executed GetAll", typeDisplay));
103-
_logger.Debug(context.Repository.TraceInfo);
104-
_logger.Debug(String.Format("{0} Results: {1} Cache Used: {2}", typeDisplay, context.NumberOfResults, context.Repository.CacheUsed));
123+
Log(String.Format("{0} Executed GetAll", typeDisplay));
124+
Log(context.Repository.TraceInfo);
125+
Log(String.Format("{0} Results: {1} Cache Used: {2}", typeDisplay, context.NumberOfResults, context.Repository.CacheUsed));
105126
}
106127

107128
public override void OnFindExecuting<T, TKey, TResult>(RepositoryQuerySingleContext<T, TKey, TResult> context)
108129
{
109130
var typeDisplay = RepositoryTypeDisplay(context.Repository);
110131

111-
_logger.Debug(String.Format("{0} Executing Find: {1}", typeDisplay, context.Specification.Predicate));
132+
Log(String.Format("{0} Executing Find: {1}", typeDisplay, context.Specification.Predicate));
112133
}
113134

114135
public override void OnFindExecuted<T, TKey, TResult>(RepositoryQuerySingleContext<T, TKey, TResult> context)
115136
{
116137
var typeDisplay = RepositoryTypeDisplay(context.Repository);
117138

118-
_logger.Debug(String.Format("{0} Executed Find: {1}", typeDisplay, context.Specification.Predicate));
119-
_logger.Debug(context.Repository.TraceInfo);
120-
_logger.Debug(String.Format("{0} Results: {1} Cache Used: {2}", typeDisplay, context.NumberOfResults, context.Repository.CacheUsed));
139+
Log(String.Format("{0} Executed Find: {1}", typeDisplay, context.Specification.Predicate));
140+
Log(context.Repository.TraceInfo);
141+
Log(String.Format("{0} Results: {1} Cache Used: {2}", typeDisplay, context.NumberOfResults, context.Repository.CacheUsed));
121142
}
122143

123144
public override void OnFindAllExecuting<T, TKey, TResult>(RepositoryQueryMultipleContext<T, TKey, TResult> context)
124145
{
125146
var typeDisplay = RepositoryTypeDisplay(context.Repository);
126147

127-
_logger.Debug(String.Format("{0} Executing FindAll: {1}", typeDisplay, context.Specification.Predicate));
148+
Log(String.Format("{0} Executing FindAll: {1}", typeDisplay, context.Specification.Predicate));
128149
}
129150

130151
public override void OnFindAllExecuted<T, TKey, TResult>(RepositoryQueryMultipleContext<T, TKey, TResult> context)
131152
{
132153
var typeDisplay = RepositoryTypeDisplay(context.Repository);
133154

134-
_logger.Debug(String.Format("{0} Executed FindAll: {1}", typeDisplay, context.Specification.Predicate));
135-
_logger.Debug(context.Repository.TraceInfo);
136-
_logger.Debug(String.Format("{0} Results: {1} Cache Used: {2}", typeDisplay, context.NumberOfResults, context.Repository.CacheUsed));
155+
Log(String.Format("{0} Executed FindAll: {1}", typeDisplay, context.Specification.Predicate));
156+
Log(context.Repository.TraceInfo);
157+
Log(String.Format("{0} Results: {1} Cache Used: {2}", typeDisplay, context.NumberOfResults, context.Repository.CacheUsed));
137158
}
138159

139160
private static string RepositoryTypeDisplay<T, TKey>(IRepository<T, TKey> repository) where T : class

SharpRepository.Repository/Aspects/RepositoryActionBaseAttribute.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ public abstract class RepositoryActionBaseAttribute : Attribute
77
{
88
protected RepositoryActionBaseAttribute()
99
{
10+
Enabled = true;
1011
Order = 9999; // high number so if they don't set it, it happens last
1112
}
1213

1314
public int Order { get; set; }
15+
public bool Enabled { get; set; }
1416

1517
public virtual void OnInitialized<T, TKey>(RepositoryActionContext<T, TKey> context) where T : class
1618
{

SharpRepository.Repository/RepositoryBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ protected RepositoryBase(ICachingStrategy<T, TKey> cachingStrategy = null)
3131
_typeName = entityType.Name;
3232

3333
_aspects = entityType.GetAllAttributes<RepositoryActionBaseAttribute>(inherit: true)
34+
.Where(x => x.Enabled)
3435
.OrderBy(x => x.Order)
3536
.ToArray();
3637

SharpRepository.Tests/Spikes/AzureTableSpikes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class AzureTableSpikes
1212
// public void TestAzureTableGet()
1313
// {
1414
// var repo = new AzureTableRepository<PoisonMessage>("UseDevelopmentStorage=true");
15-
// var item = repo.Get("QueueEmailModel", "722b6bea-d609-48e0-a4af-3ed0f5160ad9");
15+
// var item = repo.Get("QueueEmailModel", "3b80be6f-f28f-4768-99f1-6c181505ce86");
1616
//
1717
// item.ShouldNotBeNull();
1818
// }

0 commit comments

Comments
 (0)