【发布时间】:2016-02-22 19:56:50
【问题描述】:
我正在开发服务器端逻辑来处理请求并用数据响应前端服务器以及移动应用直接连接。
我已经实现了 SessionContext 类,它基本上可以确保数据库中每个被调用的服务都有匹配的会话记录(忘记密码情况等除外)。
我现在正在尝试实现事件日志记录。我想要有共同的逻辑,所以我可以记录所有请求、异常、数据等。
我想出了这段代码,但不知何故我对此感觉不太好——每种服务方法的代码都太多了。有没有什么聪明的技巧可以让我更短、更容易阅读/编码?
当前实现将使用 EventLogic 类将事件记录到事件表。在某些时候,某些事件可能与会话有关,因此我将 eventLog 作为参数传递给 SessionContext(以在事件和会话之间创建链接)。 SessionContext 在成功处理时保存实体数据...我有一种直觉,我的设计有问题。
public Session CreateUser(string email, string password, System.Net.IPAddress ipAddress)
{
using (var eventLog = new EventLogic())
{
try
{
eventLog.LogCreateUser(email, password, ipAddress);
using (var context = SessionContext.CreateUser(eventLog, email, password, ipAddress))
{
return new Session()
{
Id = context.Session.UId,
HasExpired = context.Session.IsClosed,
IsEmailVerified = context.Session.User.IsEmailVerified,
TimeCreated = context.Session.TimeCreated,
PublicUserId = CryptoHelper.GuidFromId(context.Session, context.Session.UserId, CryptoHelper.TargetTypeEnum.PublicUser),
ServerTime = context.Time
};
}
}
catch (Exception e)
{
eventLog.Exception(e);
}
}
}
【问题讨论】:
-
为什么不创建一个更通用的方法,比如 eventLog.Log(exception) 和 eventLog.Log(classname, methodname, serializedMessage, isOutgoing);或者你可以使用像 log4net 这样的可用库。
-
你需要这个日志数据做什么?
标签: c# wcf architecture