/// <summary> /// Merges each key/value pair from the context items dictionary with the ExtendedProperties /// dictionary of the <see cref="MsgEntry"/>. /// </summary> /// <param name="log"><see cref="MsgEntry"/> object that is being logged.</param> public void ProcessContextItems(MsgEntry log) { Hashtable contextItems = null; // avoid retrieval if necessary permissions are not granted to the executing assembly if (SecurityManager.IsGranted(new SecurityPermission(SecurityPermissionFlag.Infrastructure))) { try { contextItems = GetContextItems(); } catch (SecurityException) { // ignore the security exception - no item could have been set if we get the exception here. } } if (contextItems == null || contextItems.Count == 0) { return; } foreach (DictionaryEntry entry in contextItems) { string itemValue = GetContextItemValue(entry.Value); log.ExtendedProperties.Add(entry.Key.ToString(), itemValue); } }
public ViewResult Index(FormCollection forms) { //Messager.Write("OK"); //Logger.Write("OK"); MsgEntry ms = new MsgEntry(); ms.Message = "测试!"; ms.CreateTime = DateTime.Now; ms.Receiver = "JLX"; ms.Sender = "MP"; ms.Severity = System.Diagnostics.TraceEventType.Information; ms.Priority = 0; ms.Categories.Add("SMS"); ms.Categories.Add("EMAIL"); Messager.Write(ms); return View(); }
private void WriteTraceMessage(string message, string entryTitle, TraceEventType eventType) { var extendedProperties = new Dictionary<string, object>(); var entry = new MsgEntry(message, PeekLogicalOperationStack() as string, priority, eventId, eventType, entryTitle, extendedProperties); GetWriter().Write(entry); }
/// <summary> /// Validates that enough information exists to attempt executing the stored procedures /// </summary> /// <param name="msgEntry">The MsgEntry to validate.</param> /// <returns>A Boolean indicating whether the parameters for the MsgEntry configuration are valid.</returns> private bool ValidateParameters(MsgEntry msgEntry) { bool valid = true; if (writeLogStoredProcName == null || writeLogStoredProcName.Length == 0) { return false; } if (addCategoryStoredProcName == null || addCategoryStoredProcName.Length == 0) { return false; } return valid; }
/// <summary> /// Executes the WriteLog stored procedure /// </summary> /// <param name="msgEntry">The MsgEntry to store in the database.</param> /// <param name="db">An instance of the database class to use for storing the MsgEntry</param> /// <param name="transaction">The transaction that wraps around the execution calls for storing the MsgEntry</param> /// <returns>An integer for the MsgEntry Id</returns> private int ExecuteWriteLogStoredProcedure(MsgEntry msgEntry, Microsoft.Practices.EnterpriseLibrary.Data.Database db, DbTransaction transaction) { DbCommand cmd = db.GetStoredProcCommand(writeLogStoredProcName); //db.AddInParameter(cmd, "eventID", DbType.Int32, msgEntry.EventId); //db.AddInParameter(cmd, "priority", DbType.Int32, msgEntry.Priority); //db.AddParameter(cmd, "severity", DbType.String, 32, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, msgEntry.Severity.ToString()); //db.AddParameter(cmd, "title", DbType.String, 256, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, msgEntry.Title); //db.AddInParameter(cmd, "timestamp", DbType.DateTime, msgEntry.TimeStamp); //db.AddParameter(cmd, "machineName", DbType.String, 32, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, msgEntry.MachineName); //db.AddParameter(cmd, "AppDomainName", DbType.String, 512, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, msgEntry.AppDomainName); //db.AddParameter(cmd, "ProcessID", DbType.String, 256, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, msgEntry.ProcessId); //db.AddParameter(cmd, "ProcessName", DbType.String, 512, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, msgEntry.ProcessName); //db.AddParameter(cmd, "ThreadName", DbType.String, 512, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, msgEntry.ManagedThreadName); //db.AddParameter(cmd, "Win32ThreadId", DbType.String, 128, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, msgEntry.Win32ThreadId); //db.AddParameter(cmd, "message", DbType.String, 1500, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, msgEntry.Message); //if (Formatter != null) // db.AddInParameter(cmd, "formattedmessage", DbType.String, Formatter.Format(msgEntry)); //else // db.AddInParameter(cmd, "formattedmessage", DbType.String, msgEntry.Message); //db.AddOutParameter(cmd, "LogId", DbType.Int32, 4); db.ExecuteNonQuery(cmd, transaction); int logId = Convert.ToInt32(cmd.Parameters[cmd.Parameters.Count - 1].Value, CultureInfo.InvariantCulture); return logId; }
/// <summary> /// Executes the stored procedures /// </summary> /// <param name="msgEntry">The MsgEntry to store in the database</param> private void ExecuteStoredProcedure(MsgEntry msgEntry) { using (DbConnection connection = database.CreateConnection()) { connection.Open(); try { using (DbTransaction transaction = connection.BeginTransaction()) { try { int logID = Convert.ToInt32(ExecuteWriteLogStoredProcedure(msgEntry, database, transaction)); ExecuteAddCategoryStoredProcedure(msgEntry, logID, database, transaction); transaction.Commit(); } catch { transaction.Rollback(); throw; } } } finally { connection.Close(); } } }
/// <summary> /// Executes the AddCategory stored procedure /// </summary> /// <param name="msgEntry">The MsgEntry to store in the database.</param> /// <param name="logID">The unique identifer for the MsgEntry as obtained from the WriteLog Stored procedure.</param> /// <param name="db">An instance of the database class to use for storing the MsgEntry</param> /// <param name="transaction">The transaction that wraps around the execution calls for storing the MsgEntry</param> private void ExecuteAddCategoryStoredProcedure(MsgEntry msgEntry, int logID, Microsoft.Practices.EnterpriseLibrary.Data.Database db, DbTransaction transaction) { foreach (string category in msgEntry.Categories) { DbCommand cmd = db.GetStoredProcCommand(addCategoryStoredProcName); db.AddInParameter(cmd, "categoryName", DbType.String, category); db.AddInParameter(cmd, "logID", DbType.Int32, logID); db.ExecuteNonQuery(cmd, transaction); } }
/// <summary> /// Creates a new <see cref="MsgEntry"/> that is a copy of the current instance. /// </summary> /// <remarks> /// If the dictionary contained in <see cref="ExtendedProperties"/> implements <see cref="ICloneable"/>, the resulting /// <see cref="MsgEntry"/> will have its ExtendedProperties set by calling <c>Clone()</c>. Otherwise the resulting /// <see cref="MsgEntry"/> will have its ExtendedProperties set to <see langword="null"/>. /// </remarks> /// <implements>ICloneable.Clone</implements> /// <returns>A new <c>MsgEntry</c> that is a copy of the current instance.</returns> public object Clone() { MsgEntry result = new MsgEntry(); result.Message = this.Message; result.EventId = this.EventId; result.Title = this.Title; result.Severity = this.Severity; result.Priority = this.Priority; result.Sender = this.Sender; result.Receiver = this.Receiver; result.TimeStamp = this.TimeStamp; //result.MachineName = this.MachineName; //result.AppDomainName = this.AppDomainName; //result.ProcessId = this.ProcessId; //result.ProcessName = this.ProcessName; //result.ManagedThreadName = this.ManagedThreadName; //result.ActivityId = this.ActivityId; // clone categories result.Categories = new List<string>(this.Categories); // clone extended properties if (this.extendedProperties != null) result.ExtendedProperties = new Dictionary<string, object>(this.extendedProperties); // clone error messages if (this.errorMessages != null) { result.errorMessages = new StringBuilder(this.errorMessages.ToString()); } return result; }
internal void TraceData( TraceEventType eventType, int id, MsgEntry MsgEntry, TraceListenerFilter traceListenerFilter, TraceEventCache traceEventCache) { if (!ShouldTrace(eventType)) return; bool isTransfer = MsgEntry.Severity == TraceEventType.Transfer && MsgEntry.RelatedActivityId != null; foreach (TraceListener listener in traceListenerFilter.GetAvailableTraceListeners(traceListeners)) { try { if (!listener.IsThreadSafe) Monitor.Enter(listener); if (!isTransfer) { listener.TraceData(traceEventCache, Name, eventType, id, MsgEntry); } else { listener.TraceTransfer(traceEventCache, Name, id, MsgEntry.Message, MsgEntry.RelatedActivityId.Value); } instrumentationProvider.FireTraceListenerEntryWrittenEvent(); if (this.AutoFlush) { listener.Flush(); } } finally { if (!listener.IsThreadSafe) Monitor.Exit(listener); } } }
/// <summary> /// Writes trace data to the trace listeners in the <see cref="MsgSource.Listeners"/> collection that have not already been /// written to for tracing using the specified event type, event identifier, and trace data. /// </summary> /// <remarks> /// The <paramref name="traceListenerFilter"/> will be updated to reflect the trace listeners that were written to by the /// <see cref="MsgSource"/>. /// </remarks> /// <param name="eventType">The value that specifies the type of event that caused the trace.</param> /// <param name="id">A numeric identifier for the event.</param> /// <param name="MsgEntry">The <see cref="MsgEntry"/> to trace.</param> /// <param name="traceListenerFilter">The filter for already written to trace listeners.</param> public void TraceData(TraceEventType eventType, int id, MsgEntry MsgEntry, TraceListenerFilter traceListenerFilter) { this.TraceData(eventType, id, MsgEntry, traceListenerFilter, new TraceEventCache()); }
/// <summary> /// Writes trace data to the trace listeners in the <see cref="MsgSource.Listeners"/> collection using the specified /// event type, event identifier, and trace data. /// </summary> /// <param name="eventType">The value that specifies the type of event that caused the trace.</param> /// <param name="id">A numeric identifier for the event.</param> /// <param name="MsgEntry">The <see cref="MsgEntry"/> to trace.</param> public void TraceData(TraceEventType eventType, int id, MsgEntry MsgEntry) { TraceData(eventType, id, MsgEntry, new TraceListenerFilter()); }