コード例 #1
0
        public void PopulateLogEntryForCallHandler(IMethodInvocation input, 
            //List<string> categories,
            //bool includeParameters, bool includeCallStack,
            IMethodReturn result,
            long? tracingStartTicks, long? tracingEndTicks,
            long elapsedMilliseconds,
            string messageFormat,
            string moduleId,
            string functionId,
            ComponentType component)
        {
            MonitoringLogEntry logEntry = PopulateDefaultLogEntry();

            using (BackgroundWorker worker = new BackgroundWorker())
            {

                worker.DoWork += delegate(object oDoWork, DoWorkEventArgs eDoWork)
                {
                    try
                    {
                        AppContext.SetToCallContext(AppContext.Current.ToDictionary());
                        CategoryFormatter formatter = new CategoryFormatter(input.MethodBase);
                        logEntry = eDoWork.Argument as MonitoringLogEntry;
                        //foreach (string category in categories)
                        //{
                        //    logEntry.Categories.Add(formatter.FormatCategory(category));
                        //}
                        logEntry.Categories.Add(formatter.FormatCategory(LoggingCategories.Monitoring));

                        logEntry.Component = component;

                        if (!Logger.ShouldLog(logEntry))
                        {
                            return;
                        }

                        //When do the instrumentation log , we should log the parameter values information, otherwise not
                        if ((logEntry.Flag & FilterFlag.InstrumentationFlag) == FilterFlag.InstrumentationFlag)
                        {
                            //if (includeParameters)
                            {
                                logEntry.ParameterValues = GenerateValuesInfo(input.Arguments, true);
                            }
                        }

                        //if (includeCallStack)
                        //{
                        //    logEntry.CallStack = Environment.StackTrace;
                        //}

                        logEntry.TypeName = input.Target.GetType().FullName;

                        logEntry.MethodName = string.Concat(input.MethodBase.DeclaringType.FullName, ".", input.MethodBase.Name);

                        if (result == null)
                        {
                            PopulatePreLogEntry(logEntry);
                        }
                        else
                        {
                            PopulatePostLogEntry(logEntry);
                            if ((logEntry.Flag & FilterFlag.InstrumentationFlag) == FilterFlag.InstrumentationFlag)
                            {
                                int depth = 0;
                                object returnedValue = result.ReturnValue;
                                logEntry.ReturnValue = GenerateValueInfo("ReturnValue", returnedValue, (returnedValue == null) ? null : returnedValue.GetType(), ref depth);
                            }
                        }

                        logEntry.TracingStartTicks = tracingStartTicks;
                        logEntry.TracingEndTicks = tracingEndTicks;
                        logEntry.SecondsElapsed = GetSecondsElapsed(elapsedMilliseconds);
                        logEntry.Message = string.Format(Resources.Culture, messageFormat,
                                    logEntry.ExtendedActivityId,
                                    logEntry.MethodName,
                                    logEntry.TracingEndTicks,
                                    logEntry.SecondsElapsed);

                        logEntry.FunctionId = functionId;

                        logEntry.ModuleId = moduleId;

                        //if (result.ReturnValue != null && includeParameters)
                        //{
                        //    logEntry.ReturnValue = result.ReturnValue.ToString();
                        //}
                        if (result != null && result.Exception != null)
                        {
                            logEntry.Exception = result.Exception.ToString();
                        }

                        LogWriter logWriter = Logger.Writer;
                        logWriter.Write(logEntry);

                    }
                    catch (Exception ex)
                    {
                        //Swallow exception, because it is logging should not impact the business function
                        HiiPLoggingExceptionHandler handler = new HiiPLoggingExceptionHandler("Server Exception",
                            100, TraceEventType.Error,
                            "Instrumtation logging of RowUpdated failed",
                            0,
                            typeof(HiiPLoggingExceptionFormatter),
                            ComponentType.StoredProcedure,
                            Logger.Writer);
                        handler.HandleException(ex, Guid.NewGuid());
                    }

                };

                worker.RunWorkerAsync(logEntry);
            }
        }
コード例 #2
0
        public void PopulateLogEntryStoredProcedure(long? tracingStartTicks, long? tracingEndTicks,
            long elapsedMilliseconds,
            string executionMethod,
            string messageFormat,
            string moduleId,
            string functionId,
            EventArgs e)
        {
            LogWriter logWriter = Logger.Writer;
            if (!logWriter.IsTracingEnabled())
            {
                return;
            }

            MonitoringLogEntry logEntry = PopulateDefaultLogEntry();
            using (BackgroundWorker worker = new BackgroundWorker())
            {

                RowUpdatedEventArgs updatedEvent = e as RowUpdatedEventArgs;
                string[] rows = new string[0];
                if (updatedEvent != null)
                {
                    if (updatedEvent.StatementType == StatementType.Batch
                        && updatedEvent.RowCount > 0)
                    {
                        rows = new string[updatedEvent.RowCount];
                    DataRow[] tempRows ;
                        tempRows = new DataRow[updatedEvent.RowCount];
                        updatedEvent.CopyToRows(tempRows);

                        for (int i = 0; i < tempRows.Length; i++)
                        {
                            DataRow row = tempRows[i];
                            StringBuilder batchBuilder = new StringBuilder();

                            BuildRowInfo(batchBuilder, row);
                            rows[i] = batchBuilder.ToString();
                        }
                    }
                    else if (updatedEvent.Row != null)
                    {
                        rows = new string[1];
                        StringBuilder builder = new StringBuilder();

                        BuildRowInfo(builder, updatedEvent.Row);
                        rows[0] = builder.ToString();
                    }
                }
                else
                {
                    RowUpdatingEventArgs updatingEvent = e as RowUpdatingEventArgs;
                    if (updatingEvent != null)
                    {
                        rows = new string[1];
                        StringBuilder builder = new StringBuilder();

                        BuildRowInfo(builder, updatingEvent.Row);
                        rows[0] = builder.ToString();
                    }
                }
                worker.DoWork += delegate(object oDoWork, DoWorkEventArgs eDoWork)
                {
                    try
                    {
                        AppContext.SetToCallContext(AppContext.Current.ToDictionary());
                        logEntry = eDoWork.Argument as MonitoringLogEntry;

                        logEntry.Component = ComponentType.StoredProcedure;

                        logEntry.FunctionId = functionId;
                        logEntry.ModuleId = moduleId;
                        //logEntry.Categories.Add("Trace");

                        //TODO: modified by xiaofeng. Should here hardcode the category? Can it be re-used by exception logging and security logging?
                        logEntry.Categories.Add(LoggingCategories.Monitoring);

                        if (!Logger.ShouldLog(logEntry))
                        {
                            return;
                        }
                        logEntry.TracingStartTicks = tracingStartTicks;
                        logEntry.TracingEndTicks = tracingEndTicks;
                        logEntry.SecondsElapsed = GetSecondsElapsed(elapsedMilliseconds);
                        logEntry.MethodName = executionMethod;

                        logEntry.Message = string.Format(Resources.Culture, messageFormat,
                                                         logEntry.ExtendedActivityId, logEntry.MethodName, logEntry.TracingEndTicks,
                                                         logEntry.SecondsElapsed);
                        PopulateStoredProcedueDetail(logEntry, e, rows);
                    }
                    catch (Exception ex)
                    {
                        //Swallow exception, because it is logging should not impact the business function
                        HiiPLoggingExceptionHandler handler = new HiiPLoggingExceptionHandler("Server Exception",
                            100, TraceEventType.Error,
                            "Instrumtation logging of RowUpdated failed",
                            0,
                            typeof(HiiPLoggingExceptionFormatter),
                            ComponentType.StoredProcedure,
                            Logger.Writer);
                        handler.HandleException(ex, Guid.NewGuid());
                    }

                };

                worker.RunWorkerAsync(logEntry);
            }
        }