企业库日志记录不能登录SQL

时间:2015-07-02 14:25:04

标签: c# asp.net asp.net-mvc enterprise-library enterprise-library-5

我使用EntLib 6来记录一个webapp。 因为我使用的是n层,所以我希望将日志记录放在一个库中,可以从所有层访问。

我尝试运行日志记录但没有任何事情发生,所有代码都在运行,没有例外,但DB中没有任何内容。

所以这就是我所做的:

我已经使用EntLib6中提供的脚本将数据库创建到DB_BELVAL Connexion字符串下面的DB中,我将此c#测试代码放在将处理日志记录的可移植类中:

 public static void LogToDatabase()
        {
            try
            {
                //Bootstrapping logging
                DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
                IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
                LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
                Logger.SetLogWriter(logWriterFactory.Create());

                if (Logger.IsLoggingEnabled())
                {

                    LogEntry log = GetLogEntry();
                    log.Categories.Add("General");  // name of Categorie in EntLib Config editor
                    log.Priority = 5;
                    log.Severity = System.Diagnostics.TraceEventType.Information;
                    log.Message = "Hello dude, from AMLogger";
                    Logger.Write(log);

                }

            }
            catch (Exception ex)
            {
            }
        }

        private static LogEntry GetLogEntry()
        {
            LogEntry log = new LogEntry();
            log.TimeStamp = DateTime.Now;
            log.Title = "TANOLIS";
            log.Priority = 5;                       // default priority
            log.Severity = System.Diagnostics.TraceEventType.Verbose;  // default severity
            log.MachineName = "MachineName";//HttpContext.Current.Server.MachineName;
            log.ProcessId = System.Diagnostics.Process.GetCurrentProcess().Id.ToString();
            return log;
        }

从UnitTest中,我调用了可移植库的LogToDatabase()方法。 我将Unit测试的配置文件配置为:

EntLib Editor

或xml:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
      <add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        databaseInstanceName="DB_Belval" writeLogStoredProcName="WriteLog"
        addCategoryStoredProcName="General" formatter="Text Formatter" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Database Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="Database Trace Listener" />
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
          <add name="Database Trace Listener" />
        </listeners>
      </notProcessed>
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Database Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <dataConfiguration defaultDatabase="DB_Belval" />
  <connectionStrings>
    <add name="DB_Belval" connectionString="Data Source=serverIP;Initial Catalog=APP_Dev;Persist Security Info=True;User=login;Password=password"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

事实上,没有任何事情发生,但我打破了UnitTest,所有看起来都是正确的,也不例外。

感谢帮助我

**编辑1:**:logWriterFactory看起来已经加载了XML: visual_studio

编辑2:我已经编辑了存储过程[dbo]。[WriteLog]以便制作一种标志(一个INSERT INTO [dbo]。[TestLog])来知道是否调用了存储过程并且它看起来应用程序没有调用存储过程:

ALTER PROCEDURE [dbo].[WriteLog]
(
    @EventID int, 
    @Priority int, 
    @Severity nvarchar(32), 
    @Title nvarchar(256), 
    @Timestamp datetime,
    @MachineName nvarchar(32), 
    @AppDomainName nvarchar(512),
    @ProcessID nvarchar(256),
    @ProcessName nvarchar(512),
    @ThreadName nvarchar(512),
    @Win32ThreadId nvarchar(128),
    @Message nvarchar(1500),
    @FormattedMessage ntext,
    @LogId int OUTPUT
)
AS 


INSERT INTO [dbo].[TestLog]
           ([text]) VALUES ('Storeproc1')




    INSERT INTO [dbo].[Log] (
        EventID,
        Priority,
        Severity,
        Title,
        [Timestamp],
        MachineName,
        AppDomainName,
        ProcessID,
        ProcessName,
        ThreadName,
        Win32ThreadId,
        Message,
        FormattedMessage
    )
    VALUES (
        @EventID, 
        @Priority, 
        @Severity, 
        @Title, 
        @Timestamp,
        @MachineName, 
        @AppDomainName,
        @ProcessID,
        @ProcessName,
        @ThreadName,
        @Win32ThreadId,
        @Message,
        @FormattedMessage)

    SET @LogID = @@IDENTITY
    RETURN @LogID

当我启动UnitTest时,[dbo]。[TestLog]中没有新行,所以问题似乎是在C#中

1 个答案:

答案 0 :(得分:1)

看起来你已经花了一些时间撕掉你的头发。我的2美分将收集尽可能多的调试信息。我以前没有任何EL经验。

以下是我的提示;

  1. 我建议你监控目标数据库上的所有transacitons / requests。为此,只需从SSMS打开SQL事件探查器并运行它,而不使用除数据库名称之外的任何过滤器(或在整个服务器上)。顺便说一下,它是本地或远程数据库吗? 如果是远程用户,请过滤您的用户名以避免其他开发者/用户嗤之以鼻。

  2. 检查EL是否没有丢失DLL或加载异常。使用Fusion日志。如果需要,我可以为您提供有关如何使用它的信息。

  3. 从MS SDK工具(C:\ Program Files(x86)\ Microsoft SDKs \ Windows \ v10.0A \ bin \ NETFX 4.6工具)运行Fuslogvw.exe实用程序,获取最高版本。 以管理员身份运行以编辑Fusion的设置(将自定义日志路径设置为“C:\ Fuslog”) 创建目录“C:\ Fuslog”(必须存在才能启用日志记录)。 完成后禁用融合。

    1. 在Visual Studio的“例外”窗口中,禁用“启用我的代码”,启用“在异常交叉appdomain时中断或...”(最后一个与您的情况不太相关,我们永远不知道)。
    2. 您还可以启用“.NET Framework Source Stepping”,但下载符号需要一些时间。

      如果你选择这个,请确保通过显示符号窗口为EL正确加载符号。

      此处更多信息:http://blogs.msdn.com/b/saraford/archive/2008/08/26/did-you-know-how-to-load-symbols-from-the-modules-window-299.aspx

      让我们发布!