コード例 #1
0
 protected abstract string WriteEvent(LogEvent logEvent);
コード例 #2
0
        /// <summary>
        ///   Adds a record to the log.
        /// </summary>
        /// <param name="level"> The level of the message (0 = Info, 1 = Warning, 2 = Error). </param>
        /// <param name="message"> The message body. </param>
        /// <param name="ex"> The exception raised. </param>
        /// <param name="objects"> objects to seiralise to XML and store. </param>
        /// <returns> </returns>
        public string Add(int level, string message, Exception ex, object objects)
        {
            var userLoginProvider = ObjectFactory.GetInstance<IUserLoginProvider>();

            var logEvent = new LogEvent
            {
                Level = level,
                Message = message,
                User = userLoginProvider.GetUserLogin(),
                Exception = ex,
                Objects = objects,
                Date = DateTime.Now,
                ExecutingMachine = Environment.MachineName
            };

            if (HttpContext.Current != null)
            {
                if (HttpContext.Current.Items["ContextGuid"] == null)
                {
                    HttpContext.Current.Items["ContextGuid"] = Guid.NewGuid();
                }

                logEvent.ContextGuid = (Guid)HttpContext.Current.Items["ContextGuid"];
            }
            else if (OperationContext.Current != null)
            {
                var contextGuidExtension =
                    OperationContext.Current.Extensions.Find<ContextGuidExtension>();

                if (contextGuidExtension == null)
                {
                    contextGuidExtension = new ContextGuidExtension {ContextGuid = Guid.NewGuid()};
                    OperationContext.Current.Extensions.Add(contextGuidExtension);
                }

                logEvent.ContextGuid = contextGuidExtension.ContextGuid;
            }

            var stackTrace = new StackTrace();
            var stackFrames = stackTrace.GetFrames();

            foreach (StackFrame stackFrame in stackFrames)
            {
                if (stackFrame.GetMethod().DeclaringType != null)
                {
                    var declaringType = stackFrame.GetMethod().DeclaringType;
                    if (declaringType != null)
                    {
                        var logIgnoreAttribute = (LogIgnoreAttribute)
                            Attribute.GetCustomAttribute(declaringType, typeof (LogIgnoreAttribute));

                        if (logIgnoreAttribute == null)
                        {
                            logEvent.CallingMethod = stackFrame.GetMethod().Name;
                            logEvent.CallingClass = declaringType.Name;
                            logEvent.CallingAssembly = declaringType.Assembly.GetName().Name;
                            break;
                        }
                    }
                }
            }

            // Set the write event to asynchronous to improve speed 
            Task.Factory.StartNew(() => WriteEvent(logEvent));

            return "";
        }
コード例 #3
0
        protected override string WriteEvent(LogEvent logEvent)
        {
            Console.WriteLine(logEvent.Message);

            var cn = new SqlConnection(_connectionString);
            var cmd = new SqlCommand("INSERT INTO LogEvent " +
                "([Date], [Level], [Message], [User], [Exception], [Objects], " +
                    "[ExecutingMachine], [CallingAssembly], [CallingClass], [CallingMethod], " +
                        "[ContextGuid]) VALUES (" +
                            "@Date, @Level, @Message, @User, @Exception, @Objects, " +
                                "@ExecutingMachine, @CallingAssembly, @CallingClass, @CallingMethod, " +
                                    "@ContextGuid); SELECT SCOPE_IDENTITY() AS LogEventId; ", cn);

            cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = logEvent.Date;

            cmd.Parameters.Add("@Level", SqlDbType.Int).Value = logEvent.Level;

            cmd.Parameters.Add("@Message", SqlDbType.VarChar, 100).Value = SetObjectToNull(logEvent.Message);

            cmd.Parameters.Add("@User", SqlDbType.VarChar, 50).Value = SetObjectToNull(logEvent.User);

            cmd.Parameters.Add("@Exception", SqlDbType.VarChar, 2147483647);
            if (logEvent.Exception != null)
            {
                cmd.Parameters["@Exception"].Value = logEvent.Exception.ToString();
            }
            else
            {
                cmd.Parameters["@Exception"].Value = DBNull.Value;
            }

            cmd.Parameters.Add("@Objects", SqlDbType.Xml);
            if (logEvent.Objects != null)
            {
                cmd.Parameters["@Objects"].Value = DataContractObjectSerializer.Serialize(logEvent.Objects);
            }
            else
            {
                cmd.Parameters["@Objects"].Value = DBNull.Value;
            }

            cmd.Parameters.Add("@ExecutingMachine", SqlDbType.VarChar, 50).Value =
                SetObjectToNull(logEvent.ExecutingMachine);

            cmd.Parameters.Add("@CallingAssembly", SqlDbType.VarChar, 50).Value =
                SetObjectToNull(logEvent.CallingAssembly);

            cmd.Parameters.Add("@CallingClass", SqlDbType.VarChar, 50).Value = SetObjectToNull(logEvent.CallingClass);

            cmd.Parameters.Add("@CallingMethod", SqlDbType.VarChar, 50).Value = SetObjectToNull(logEvent.CallingMethod);

            cmd.Parameters.Add("@ContextGuid", SqlDbType.VarChar, 50).Value =
                SetObjectToNull(logEvent.ContextGuid.ToString());

            string logEventId = null;
            try
            {
                cn.Open();
                var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                if (dr.Read())
                {
                    logEventId = dr["LogEventId"].ToString();
                }
            }
            finally
            {
                cmd.Dispose();
                cn.Close();
            }

            return logEventId;
        }