使用EnterpriseLibrary将数据记录到数据库

时间:2013-09-10 17:00:36

标签: c# .net enterprise-library

您好我正在尝试使用数据库作为存储将EnterprisLibrary Logging块集成到我的应用程序中。这是我到目前为止所拥有的:

我已经破坏了数据库的脚本并创建了一个名为e-shop.Logging的数据库 它有3个表Category,CategoryLog和Log。

我已在我的网络配置中创建了此配置:

enter image description here enter image description here enter image description here enter image description here

在我的代码中,我有这个配置类:

public static class LoggerBootstrapper
{
    public static void Start()
    {
        DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());

        Logger.SetLogWriter(new LogWriterFactory().Create());
    }
}

这在我的WebApi项目中的Global.asax中调用。

这是我在整个应用程序中使用的类:

[InjectorRegistration(typeof(IExceptionLogger))]
public class ExceptionLogger : IExceptionLogger
{
    public void LogServerException(Exception exception)
    {
        string logger = LoggerCategories.ServerLogger.ToString();
        Logger.Write(exception, LoggerCategories.ServerLogger.ToString());
    }

    public void LogClientException(Exception exception)
    {
        Logger.Write(exception, LoggerCategories.ClientLogger.ToString());
    }
}
 public enum LoggerCategories
{
    ServerLogger,
    ClientLogger
}

当我调用LogServerException或LogClientException时,数据get存储在Log表中,但没有任何内容存储在Category和CategoryLog表中。

我首先认为我必须自己添加类别,所以我添加了这两个类别: ServerLogger ClientLogger

当然它没有用。我已经看到这个配置,当你添加一个类别时,它会自动添加到Category表中,当添加一个异常时,类别和日志之间的映射会自动完成抛出CategoryLog。

有谁知道我在做错了什么? 为什么当我添加异常数据时,只在日志表中添加了get,而另外两个表没有更新?

1 个答案:

答案 0 :(得分:0)

在抛出XML后,我发现添加类别的存储过程没有设置正确。我将它设置为ServerLogger,实际的存储过程名称是AddCateogry。