Example #1
0
        /// <summary>
        /// Adds Sql Logger to LoggerFactory
        /// </summary>
        /// <param name="loggerFactory">LoggerFactory Instance</param>
        /// <param name="settings">Sql Logger Settings</param>
        /// <param name="filter">If specified it will override all defined switches.</param>
        /// <returns></returns>
        public static ILoggerFactory AddSqlServerLogger(this ILoggerFactory loggerFactory,
                                                        ISqlServerLoggerSettings settings,
                                                        Func <string, LogLevel, bool> filter = null)
        {
            loggerFactory.AddProvider(new SqlServerLogProvider(settings, filter));

            return(loggerFactory);
        }
        public SqlServerLogger(ISqlServerLoggerSettings settings, string categoryName, Func <string, LogLevel, bool> filter = null, Func <LogLevel, EventId, object, Exception, SqlCommand> eventDataFormatter = null)
        {
            try
            {
                m_Settings = settings;

                m_CategoryName = categoryName;
                if (filter == null)
                {
                    m_Filter = filter ?? ((category, logLevel) => true);
                }
                else
                {
                    m_Filter = filter;
                }

                m_IgnoreLoggingErrors = settings.IgnoreLoggingErrors;

                using (SqlConnection conn = new SqlConnection(m_Settings.ConnectionString))
                {
                    conn.Open();
                    SqlCommandFormatter = SqlCommandFormatter == null ? defaultSqlCmdFormatter : eventDataFormatter;

                    if (!tableExists(conn))
                    {
                        if (!m_Settings.CreateTblIfNotExist)
                        {
                            handleError(new Exception("No table exists. You can enable automatic table creation by setting 'CreateTblIfNotExist' to true"));
                        }
                        else
                        {
                            createSqlTable(conn);
                        }
                    }
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                handleError(ex);
            }
        }
        private Func <string, LogLevel, bool> getFilter(string name, ISqlServerLoggerSettings settings)
        {
            if (m_Filter != null)
            {
                return(m_Filter);
            }

            if (settings != null)
            {
                foreach (var prefix in getKeyPrefixes(name))
                {
                    LogLevel level;
                    if (settings.Switches.TryGetValue(prefix, out level))
                    {
                        return((n, l) => l >= level);
                    }
                }
            }

            return((n, l) =>
                   false);
        }
 /// <summary>
 /// Creates SQL Logger Provider
 /// </summary>
 /// <param name="settings">Logger Settings</param>
 /// <param name="filter">TODO..</param>
 public SqlServerLogProvider(ISqlServerLoggerSettings settings, Func <string, LogLevel, bool> filter)
 {
     this.m_Filter   = filter;
     this.m_Settings = settings;
 }