/// <summary> /// Executes the stored procedure to insert a trace message into SQL. /// </summary> /// <param name="message">Trace message to insert into the database.</param> /// <param name="helpLink">Help link to insert into the database.</param> /// <param name="source">Source of the trace message to insert into the database.</param> /// <param name="stackTrace">Stack trace of the trace message to insert into the database.</param> /// <param name="traceData">Data associated with the trace event.</param> /// <param name="traceEventType">Type of the trace event.</param> /// <param name="traceId">Id of the trace message to insert into the database.</param> /// <param name="correlationId">Optional correlation Id of the trace message to insert into the database.</param> /// <param name="correlationIndex">Optional correlation index of the trace message to insert into the database.</param> /// <param name="processId">Process Id to insert into the database.</param> /// <param name="threadId">Thread Id to insert into the database.</param> /// <returns>Primary key Id of the record that was inserted into that database.</returns> private long InsertMessage( string message, string helpLink, string source, string stackTrace, XmlDocument traceData, TraceEventType traceEventType, int traceId, Nullable<Guid> correlationId, Nullable<short> correlationIndex, int processId, string threadId) { string connectionString = this.GetConnectionString(); var assembly = Assembly.GetExecutingAssembly(); var assemblyName = assembly.GetName(); var repository = new ApplicationLogRepository(connectionString); var applicationLog = new ApplicationLogDao { AppDomainName = AppDomain.CurrentDomain.FriendlyName, Assembly = new AssemblyDao { AssemblyFullName = assemblyName.FullName, AssemblyName = assemblyName.Name, VersionBuild = assemblyName.Version.Build, VersionMajor = assemblyName.Version.Major, VersionMinor = assemblyName.Version.Minor, VersionRevision = assemblyName.Version.Revision }, CorrelationId = correlationId, CorrelationIndex = correlationIndex, Data = traceData, HelpLink = helpLink, MachineName = Environment.MachineName, Message = message, ProcessId = processId, Source = source, StackTrace = stackTrace, ThreadId = threadId, TraceEventType = traceEventType, TraceId = traceId, TraceListenerName = this.Name }; long applicationLogId = repository.Create(applicationLog); return applicationLogId; }
/// <summary> /// Creates a new application log. /// </summary> /// <param name="applicationLog">New application log to create in the database.</param> /// <returns>Id of the new application log record.</returns> public long Create(ApplicationLogDao applicationLog) { using (var connection = new SqlConnection(this._connectionString)) using (var command = connection.CreateCommand()) { command.CommandType = CommandType.Text; command.CommandText = SqlQueries.InsertApplicationLog; var assembly = Assembly.GetExecutingAssembly(); var assemblyName = assembly.GetName(); string traceDataXmlString = null; if (null != applicationLog.Data) { traceDataXmlString = applicationLog.Data.InnerXml; } command.Parameters.Add(new SqlParameter { ParameterName = "inMessage", DbType = DbType.String, Value = this.ValueOrDbNull(applicationLog.Message) }); command.Parameters.Add(new SqlParameter { ParameterName = "inHelpLink", DbType = DbType.String, Size = 2083, Value = this.ValueOrDbNull(applicationLog.HelpLink) }); command.Parameters.Add(new SqlParameter { ParameterName = "inSource", DbType = DbType.String, Value = this.ValueOrDbNull(applicationLog.Source) }); command.Parameters.Add(new SqlParameter { ParameterName = "inStackTrace", DbType = DbType.String, Value = this.ValueOrDbNull(applicationLog.StackTrace) }); command.Parameters.Add(new SqlParameter { ParameterName = "inTraceData", DbType = DbType.Xml, Value = this.ValueOrDbNull(traceDataXmlString) }); command.Parameters.Add(new SqlParameter { ParameterName = "inTraceId", DbType = DbType.Int32, Value = this.ValueOrDbNull(applicationLog.TraceId) }); command.Parameters.Add(new SqlParameter { ParameterName = "inCorrelationId", DbType = DbType.Guid, Value = this.ValueOrDbNull(applicationLog.CorrelationId) }); command.Parameters.Add(new SqlParameter { ParameterName = "inCorrelationIndex", DbType = DbType.Int32, Value = this.ValueOrDbNull(applicationLog.CorrelationIndex) }); command.Parameters.Add(new SqlParameter { ParameterName = "inAssemblyName", DbType = DbType.String, Value = assemblyName.Name }); command.Parameters.Add(new SqlParameter { ParameterName = "inAssemblyFullName", DbType = DbType.String, Value = assemblyName.FullName }); command.Parameters.Add(new SqlParameter { ParameterName = "inAssemblyVersionMajor", DbType = DbType.Int32, Value = assemblyName.Version.Major }); command.Parameters.Add(new SqlParameter { ParameterName = "inAssemblyVersionMinor", DbType = DbType.Int32, Value = assemblyName.Version.Minor }); command.Parameters.Add(new SqlParameter { ParameterName = "inAssemblyVersionBuild", DbType = DbType.Int32, Value = assemblyName.Version.Build }); command.Parameters.Add(new SqlParameter { ParameterName = "inAssemblyVersionRevision", DbType = DbType.Int32, Value = assemblyName.Version.Revision }); command.Parameters.Add(new SqlParameter { ParameterName = "inMachineName", DbType = DbType.String, Value = Environment.MachineName }); command.Parameters.Add(new SqlParameter { ParameterName = "inAppDomainName", DbType = DbType.String, Value = AppDomain.CurrentDomain.FriendlyName }); command.Parameters.Add(new SqlParameter { ParameterName = "inProcessId", DbType = DbType.Int32, Value = this.ValueOrDbNull(applicationLog.ProcessId) }); command.Parameters.Add(new SqlParameter { ParameterName = "inThreadId", DbType = DbType.String, Size = 255, Value = this.ValueOrDbNull(applicationLog.ThreadId) }); command.Parameters.Add(new SqlParameter { ParameterName = "inTraceEventType", DbType = DbType.Int32, Value = (int) applicationLog.TraceEventType }); command.Parameters.Add(new SqlParameter { ParameterName = "inTraceListenerName", DbType = DbType.String, Value = this.ValueOrDbNull(applicationLog.TraceListenerName) }); var recordId = new SqlParameter { ParameterName = "outMessageLogId", DbType = DbType.Int64, Direction = ParameterDirection.Output }; command.Parameters.Add(recordId); if (ConnectionState.Open != connection.State) { connection.Open(); } command.ExecuteNonQuery(); long messageId = (long) recordId.Value; return messageId; } }