在我正在开发的应用程序中,我们正在使用InProc ASP.NET SessionState对象。
我想找到一种方法来记录消息,只要某些代码读取或写入Session对象。
我知道Item []索引,我想我应该以某种方式注入代码..或者可能继承HttpSessionState类?
选项,想法,经验......?
答案 0 :(得分:2)
只是一个补充。
(编辑:我把它变得更通用了)
首先,抽象包装的接口
interface ISessionStateItemCollectionWrapper : ISessionStateItemCollection
{
ISessionStateItemCollection WrappedCollection { get; set; }
}
因为我想在读取或写入会话项时记录,所以我为ISessionStateItemCollection添加了一个包装器
public class SessionStateItemCollectionWithInstrumentation : ISessionStateItemCollectionWrapper
{
public ISessionStateItemCollection WrappedCollection { get; set; }
//...
//...
//...
public object this[string name]
{
get
{
LogRead();
return _wrappedCollection[name];
}
set
{
LogWrite();
_wrappedCollection[name] = value;
}
}
//...
//More log on some methods
//...
}
然后,为了注入它,我继承了SessionStateStoreData
class SessionStoreDataItemsInjector<T> : SessionStateStoreData where T:ISessionStateItemCollectionWrapper, new()
{
public SessionStoreDataItemsInjector(SessionStateStoreData wrappedData)
: base(new T()
{
WrappedCollection = wrappedData.Items
}, wrappedData.StaticObjects, wrappedData.Timeout)
{
}
}
然后我将它连接到Don提供的课程
abstract class InProcSessionStateInjected<T> : SessionStateStoreProviderBase where T : ISessionStateItemCollectionWrapper, new()
{
private SessionStateStoreProviderBase inProcSessionStore;
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
var inProcSessionStoreType = typeof(SessionStateStoreProviderBase).Assembly.GetType("System.Web.SessionState.InProcSessionStateStore");
inProcSessionStore = (SessionStateStoreProviderBase)Activator.CreateInstance(inProcSessionStoreType);
inProcSessionStore.Initialize(name, config);
}
public override SessionStateStoreData CreateNewStoreData(HttpContext context, int timeout)
{
var sessionStateStoreData = inProcSessionStore.CreateNewStoreData(context, timeout);
return sessionStateStoreData.Items.GetType() != typeof(T) ? new SessionStoreDataItemsInjector<T>(sessionStateStoreData) : sessionStateStoreData;
}
public override SessionStateStoreData GetItem(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)
{
var sessionStateStoreData = inProcSessionStore.GetItem(context, id, out locked, out lockAge, out lockId, out actions);
if (sessionStateStoreData != null && sessionStateStoreData.Items.GetType() != typeof(T))
{
return new SessionStoreDataItemsInjector<T>(sessionStateStoreData);
}
return sessionStateStoreData;
}
public override SessionStateStoreData GetItemExclusive(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)
{
var sessionStateStoreData = inProcSessionStore.GetItemExclusive(context, id, out locked, out lockAge, out lockId, out actions);
if (sessionStateStoreData != null && sessionStateStoreData.Items.GetType() != typeof(T))
{
return new SessionStoreDataItemsInjector<T>(sessionStateStoreData);
}
return sessionStateStoreData;
}
答案 1 :(得分:1)
您可以创建自定义SessionStateProvider,并且可以从该提供程序中实例化InProcSessionStateStore的实例,并使用它来执行与会话存储处理相关的所有工作。
在此自定义SessionStateProvider中,您可以执行所有日志记录。
所以这就是你必须要做的事情:
创建一个继承SessionStateStoreProviderBase类的新类,假设它名为“MySessionStateProvider”
实现所有抽象类方法。
从类的构造函数中,使用Reflection创建InProcSessionStateStore类的实例(当用于InProc时,ASP.NET使用它来存储会话状态 - 您的情况) 将此实例存储在变量
以下是示例代码(以及指向其他人如何创建自定义提供程序的链接,如果您需要有更多信息的工作示例)。
https://msdn.microsoft.com/en-us/library/system.web.sessionstate.sessionstatestoreproviderbase(v=vs.110).aspx
http://www.codeproject.com/Articles/102000/ASP-NET-Custom-Session-Store-Provider-compatible-w
的Web.Config
preprocessor
MySessionStateProvider类代码:
<sessionState mode="Custom" customProvider="custom_provider" ... >
<providers>
<add name="custom_provider" type="Xxxx.xxx.MySessionStateProvider"/>
</providers>
</sessionState>