Ejemplo n.º 1
0
        /// <summary>
        /// Adds the given list of DatabaseParameterInfo to the given IDbCommand after transforming them into IDbDataParameters.
        /// </summary>
        /// <param name="command">The IDbCommand to add parameters to</param>
        /// <param name="databaseParameterInfos">The list of DatabaseParameterInfo to transform into IDbDataParameters and to add to the IDbCommand</param>
        /// <param name="logEvent">The log event to base the parameter's layout rendering on.</param>
        private void AddParametersToCommand(IDbCommand command, IList <DatabaseParameterInfo> databaseParameterInfos, LogEventInfo logEvent)
        {
            for (int i = 0; i < databaseParameterInfos.Count; ++i)
            {
                DatabaseParameterInfo par = databaseParameterInfos[i];
                IDbDataParameter      p   = command.CreateParameter();
                p.Direction = ParameterDirection.Input;
                if (par.Name != null)
                {
                    p.ParameterName = par.Name;
                }

                if (par.Size != 0)
                {
                    p.Size = par.Size;
                }

                if (par.Precision != 0)
                {
                    p.Precision = par.Precision;
                }

                if (par.Scale != 0)
                {
                    p.Scale = par.Scale;
                }

                string stringValue = RenderLogEvent(par.Layout, logEvent);

                p.Value = stringValue;
                command.Parameters.Add(p);

                InternalLogger.Trace("  DatabaseTarget: Parameter: '{0}' = '{1}' ({2})", p.ParameterName, p.Value, p.DbType);
            }
        }
Ejemplo n.º 2
0
        private void WriteEventToDatabase(LogEventInfo logEvent)
        {
            //Always suppress transaction so that the caller does not rollback loggin if they are rolling back their transaction.
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Suppress))
            {
                EnsureConnectionOpen(BuildConnectionString(logEvent));

                using (IDbCommand command = _activeConnection.CreateCommand())
                {
                    command.CommandText = RenderLogEvent(CommandText, logEvent);
                    command.CommandType = CommandType;

                    InternalLogger.Trace("DatabaseTarget(Name={0}): Executing {1}: {2}", Name, command.CommandType, command.CommandText);

                    for (int i = 0; i < Parameters.Count; ++i)
                    {
                        DatabaseParameterInfo par = Parameters[i];
                        IDbDataParameter      p   = command.CreateParameter();
                        p.Direction = ParameterDirection.Input;
                        if (par.Name != null)
                        {
                            p.ParameterName = par.Name;
                        }

                        if (par.Size != 0)
                        {
                            p.Size = par.Size;
                        }

                        if (par.Precision != 0)
                        {
                            p.Precision = par.Precision;
                        }

                        if (par.Scale != 0)
                        {
                            p.Scale = par.Scale;
                        }

                        string stringValue = RenderLogEvent(par.Layout, logEvent);

                        p.Value = stringValue;
                        command.Parameters.Add(p);

                        InternalLogger.Trace("  DatabaseTarget: Parameter: '{0}' = '{1}' ({2})", p.ParameterName, p.Value, p.DbType);
                    }

                    int result = command.ExecuteNonQuery();
                    InternalLogger.Trace("DatabaseTarget(Name={0}): Finished execution, result = {1}", Name, result);
                }

                //not really needed as there is no transaction at all.
                transactionScope.Complete();
            }
        }
Ejemplo n.º 3
0
Archivo: Example.cs Proyecto: ExM/NLog
    static void Main(string[] args)
    {
        DatabaseTarget target = new DatabaseTarget();
        DatabaseParameterInfo param;

        target.DBProvider = "mssql";
        target.DBHost = ".";
        target.DBUserName = "******";
        target.DBPassword = "******";
        target.DBDatabase = "databasename";
        target.CommandText = "insert into LogTable(time_stamp,level,logger,message) values(@time_stamp, @level, @logger, @message);";

        param = new DatabaseParameterInfo();
        param.Name = "@time_stamp";
        param.Layout = "${date}";
        target.Parameters.Add(param);

        param = new DatabaseParameterInfo();
        param.Name = "@level";
        param.Layout = "${level}";
        target.Parameters.Add(param);

        param = new DatabaseParameterInfo();
        param.Name = "@logger";
        param.Layout = "${logger}";
        target.Parameters.Add(param);

        param = new DatabaseParameterInfo();
        param.Name = "@message";
        param.Layout = "${message}";
        target.Parameters.Add(param);

        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);

        Logger logger = LogManager.GetLogger("Example");
        logger.Debug("log message");
    }
 /// <summary>
 /// Adds the elements of an array to the end of this DatabaseParameterInfoCollection.
 /// </summary>
 /// <param name="items">
 /// The array whose elements are to be added to the end of this DatabaseParameterInfoCollection.
 /// </param>
 public virtual void AddRange(DatabaseParameterInfo[]items)
 {
     foreach (DatabaseParameterInfo item in items)
     {
         this.List.Add(item);
     }
 }
Ejemplo n.º 5
0
        static void nlog_to_db() {
            ensure_db_exists();

            DatabaseTarget target = new DatabaseTarget();
            DatabaseParameterInfo param;

            // just in case issues with db.
            LogManager.ThrowExceptions = true;

//            target.DBProvider = "System.Data.SQLite";
            target.DBProvider = "System.Data.SQLite.SQLiteConnection, System.Data.SQLite";
            //target.ConnectionString = "Data Source=${basedir}\\Log.db3;Version=3;";
            target.ConnectionString = "Data Source=Log.db3;Version=3;";
            target.CommandType = CommandType.Text;
            target.CommandText = "insert into Log(time_stamp,level,logger,message) values(@time_stamp, @level, @logger, @message);";

            param = new DatabaseParameterInfo();
            param.Name = "@time_stamp";
            param.Layout = "${date}";
            target.Parameters.Add(param);

            param = new DatabaseParameterInfo();
            param.Name = "@level";
            param.Layout = "${level}";
            target.Parameters.Add(param);

            param = new DatabaseParameterInfo();
            param.Name = "@logger";
            param.Layout = "${logger}";
            target.Parameters.Add(param);

            param = new DatabaseParameterInfo();
            param.Name = "@message";
            param.Layout = "${message}";
            target.Parameters.Add(param);

            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
            
        }
 /// <summary>
 /// Removes the first occurrence of a specific DatabaseParameterInfo from this DatabaseParameterInfoCollection.
 /// </summary>
 /// <param name="value">
 /// The DatabaseParameterInfo value to remove from this DatabaseParameterInfoCollection.
 /// </param>
 public virtual void Remove(DatabaseParameterInfo value)
 {
     this.List.Remove(value);
 }
 /// <summary>
 /// Initializes a new instance of the DatabaseParameterInfoCollection class, containing elements
 /// copied from an array.
 /// </summary>
 /// <param name="items">
 /// The array whose elements are to be added to the new DatabaseParameterInfoCollection.
 /// </param>
 public DatabaseParameterInfoCollection(DatabaseParameterInfo[]items)
 {
     this.AddRange(items);
 }
 /// <summary>
 /// Return the zero-based index of the first occurrence of a specific value
 /// in this DatabaseParameterInfoCollection
 /// </summary>
 /// <param name="value">
 /// The DatabaseParameterInfo value to locate in the DatabaseParameterInfoCollection.
 /// </param>
 /// <returns>
 /// The zero-based index of the first occurrence of the _ELEMENT value if found;
 /// -1 otherwise.
 /// </returns>
 public virtual int IndexOf(DatabaseParameterInfo value)
 {
     return this.List.IndexOf(value);
 }
 /// <summary>
 /// Inserts an element into the DatabaseParameterInfoCollection at the specified index
 /// </summary>
 /// <param name="index">
 /// The index at which the DatabaseParameterInfo is to be inserted.
 /// </param>
 /// <param name="value">
 /// The DatabaseParameterInfo to insert.
 /// </param>
 public virtual void Insert(int index, DatabaseParameterInfo value)
 {
     this.List.Insert(index, value);
 }
 /// <summary>
 /// Adds an instance of type DatabaseParameterInfo to the end of this DatabaseParameterInfoCollection.
 /// </summary>
 /// <param name="value">
 /// The DatabaseParameterInfo to be added to the end of this DatabaseParameterInfoCollection.
 /// </param>
 public virtual void Add(DatabaseParameterInfo value)
 {
     this.List.Add(value);
 }
 /// <summary>
 /// Determines whether a specfic DatabaseParameterInfo value is in this DatabaseParameterInfoCollection.
 /// </summary>
 /// <param name="value">
 /// The DatabaseParameterInfo value to locate in this DatabaseParameterInfoCollection.
 /// </param>
 /// <returns>
 /// true if value is found in this DatabaseParameterInfoCollection;
 /// false otherwise.
 /// </returns>
 public virtual bool Contains(DatabaseParameterInfo value)
 {
     return this.List.Contains(value);
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Removes the first occurrence of a specific DatabaseParameterInfo from this DatabaseParameterInfoCollection.
 /// </summary>
 /// <param name="value">
 /// The DatabaseParameterInfo value to remove from this DatabaseParameterInfoCollection.
 /// </param>
 public virtual void Remove(DatabaseParameterInfo value)
 {
     this.List.Remove(value);
 }
Ejemplo n.º 13
0
        private DatabaseTarget ConfigureDatabaseTarget()
        {
            // Create database target
            DatabaseTarget eventLogDatabaseTarget = new DatabaseTarget();
            try
            {
                eventLogDatabaseTarget.Name = "EventLogDatabase";

                DatabaseParameterInfo param;

                string connectionString = _logWriterConfiguration.GetEventLogConnectionString();

                SqlConnectionStringBuilder csBuilder = new SqlConnectionStringBuilder(connectionString);

                eventLogDatabaseTarget.DBProvider = "mssql";
                eventLogDatabaseTarget.DBHost = csBuilder.DataSource;
                if (!csBuilder.IntegratedSecurity)
                {
                    eventLogDatabaseTarget.DBUserName = csBuilder.UserID;
                    eventLogDatabaseTarget.DBPassword = csBuilder.Password;
                }
                eventLogDatabaseTarget.DBDatabase = csBuilder.InitialCatalog;

                eventLogDatabaseTarget.CommandText =
                    "exec [InsertLog] @SeverityID, @Title, @Timestamp, @MachineName, @AppDomainName, @ProcessName, @ApplicationName, @CategoryName, @ProcessAccountName, @IdentityName, @Message, @ReferenceID, @MessageType, @AdditionalInfo1, @AdditionalInfo2, @AlertCondition";

                param = new DatabaseParameterInfo();
                param.Name = "@SeverityID";
                param.Layout = "${event-context:item=LevelID}";
                eventLogDatabaseTarget.Parameters.Add(param);

                param = new DatabaseParameterInfo();
                param.Name = "@Title";
                param.Layout = "${event-context:item=Title}";
                eventLogDatabaseTarget.Parameters.Add(param);

                param = new DatabaseParameterInfo();
                param.Name = "@Timestamp";
                param.Layout = "${event-context:item=EventDateTime}";
                eventLogDatabaseTarget.Parameters.Add(param);

                param = new DatabaseParameterInfo();
                param.Name = "@MachineName";
                param.Layout = "${machinename}";
                eventLogDatabaseTarget.Parameters.Add(param);

                param = new DatabaseParameterInfo();
                param.Name = "@AppDomainName";
                param.Layout = "${event-context:item=AppDomainName}";
                eventLogDatabaseTarget.Parameters.Add(param);

                param = new DatabaseParameterInfo();
                param.Name = "@ProcessName";
                param.Layout = "${processname}";
                eventLogDatabaseTarget.Parameters.Add(param);

                param = new DatabaseParameterInfo();
                param.Name = "@ApplicationName";
                param.Layout = "${event-context:item=ApplicationName}";
                eventLogDatabaseTarget.Parameters.Add(param);

                param = new DatabaseParameterInfo();
                param.Name = "@CategoryName";
                param.Layout = "${event-context:item=Category}";
                eventLogDatabaseTarget.Parameters.Add(param);

                param = new DatabaseParameterInfo();
                param.Name = "@ProcessAccountName";
                param.Layout = "${windows-identity}";
                eventLogDatabaseTarget.Parameters.Add(param);

                param = new DatabaseParameterInfo();
                param.Name = "@IdentityName";
                param.Layout = "${event-context:item=Identity}";
                eventLogDatabaseTarget.Parameters.Add(param);

                param = new DatabaseParameterInfo();
                param.Name = "@Message";
                param.Layout = "${message:withException=true}";
                eventLogDatabaseTarget.Parameters.Add(param);

                param = new DatabaseParameterInfo();
                param.Name = "@ReferenceID";
                param.Layout = "${event-context:item=ReferenceID}";
                eventLogDatabaseTarget.Parameters.Add(param);

                param = new DatabaseParameterInfo();
                param.Name = "@MessageType";
                param.Layout = "${event-context:item=MessageType}";
                eventLogDatabaseTarget.Parameters.Add(param);

                param = new DatabaseParameterInfo();
                param.Name = "@AdditionalInfo1";
                param.Layout = "${event-context:item=AdditionalInfo1}";
                eventLogDatabaseTarget.Parameters.Add(param);

                param = new DatabaseParameterInfo();
                param.Name = "@AdditionalInfo2";
                param.Layout = "${event-context:item=AdditionalInfo2}";
                eventLogDatabaseTarget.Parameters.Add(param);

                param = new DatabaseParameterInfo();
                param.Name = "@AlertCondition";
                param.Layout = "${event-context:item=AlertCondition}";
                eventLogDatabaseTarget.Parameters.Add(param);
            }
            catch (Exception ex)
            {
                InternalLogger.Error("*** " + ex.ToString());
                throw;
            }

            return eventLogDatabaseTarget;
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Inserts an element into the DatabaseParameterInfoCollection at the specified index
 /// </summary>
 /// <param name="index">
 /// The index at which the DatabaseParameterInfo is to be inserted.
 /// </param>
 /// <param name="value">
 /// The DatabaseParameterInfo to insert.
 /// </param>
 public virtual void Insert(int index, DatabaseParameterInfo value)
 {
     this.List.Insert(index, value);
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Return the zero-based index of the first occurrence of a specific value
 /// in this DatabaseParameterInfoCollection
 /// </summary>
 /// <param name="value">
 /// The DatabaseParameterInfo value to locate in the DatabaseParameterInfoCollection.
 /// </param>
 /// <returns>
 /// The zero-based index of the first occurrence of the _ELEMENT value if found;
 /// -1 otherwise.
 /// </returns>
 public virtual int IndexOf(DatabaseParameterInfo value)
 {
     return this.List.IndexOf(value);
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Determines whether a specfic DatabaseParameterInfo value is in this DatabaseParameterInfoCollection.
 /// </summary>
 /// <param name="value">
 /// The DatabaseParameterInfo value to locate in this DatabaseParameterInfoCollection.
 /// </param>
 /// <returns>
 /// true if value is found in this DatabaseParameterInfoCollection;
 /// false otherwise.
 /// </returns>
 public virtual bool Contains(DatabaseParameterInfo value)
 {
     return this.List.Contains(value);
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Adds an instance of type DatabaseParameterInfo to the end of this DatabaseParameterInfoCollection.
 /// </summary>
 /// <param name="value">
 /// The DatabaseParameterInfo to be added to the end of this DatabaseParameterInfoCollection.
 /// </param>
 public virtual void Add(DatabaseParameterInfo value)
 {
     this.List.Add(value);
 }
Ejemplo n.º 18
0
        private DatabaseTarget ConfigureSecurityActionDatabaseTarget()
        {
            // Create database target
            DatabaseTarget eventLogDatabaseTarget = new DatabaseTarget();
            eventLogDatabaseTarget.Name = "SecurityActionEventLogDatabase";

            DatabaseParameterInfo param;

            string connectionString = _logWriterConfiguration.GetEventLogConnectionString();

            SqlConnectionStringBuilder csBuilder = new SqlConnectionStringBuilder(connectionString);

            eventLogDatabaseTarget.DBProvider = "mssql";
            eventLogDatabaseTarget.DBHost = csBuilder.DataSource;
            if (!csBuilder.IntegratedSecurity)
            {
                eventLogDatabaseTarget.DBUserName = csBuilder.UserID;
                eventLogDatabaseTarget.DBPassword = csBuilder.Password;
            }
            eventLogDatabaseTarget.DBDatabase = csBuilder.InitialCatalog;

            eventLogDatabaseTarget.CommandText = "exec [LogSecurityEvent] @action, @targetAccount, @actionBy, @actionByIPAddress, @serverIPAddress, @serverName, @application, @description";

            param = new DatabaseParameterInfo();
            param.Name = "@action";
            param.Layout = "${event-context:item=Action}";
            eventLogDatabaseTarget.Parameters.Add(param);

            param = new DatabaseParameterInfo();
            param.Name = "@targetAccount";
            param.Layout = "${event-context:item=TargetAccount}";
            eventLogDatabaseTarget.Parameters.Add(param);

            param = new DatabaseParameterInfo();
            param.Name = "@actionBy";
            param.Layout = "${event-context:item=ActionBy}";
            eventLogDatabaseTarget.Parameters.Add(param);

            param = new DatabaseParameterInfo();
            param.Name = "@actionByIPAddress";
            param.Layout = "${event-context:item=ActionByIPAddress}";
            eventLogDatabaseTarget.Parameters.Add(param);

            param = new DatabaseParameterInfo();
            param.Name = "@serverIPAddress";
            param.Layout = "${event-context:item=ServerIPAddress}";
            eventLogDatabaseTarget.Parameters.Add(param);

            param = new DatabaseParameterInfo();
            param.Name = "@serverName";
            param.Layout = "${machinename}";
            eventLogDatabaseTarget.Parameters.Add(param);

            param = new DatabaseParameterInfo();
            param.Name = "@application";
            param.Layout = "${event-context:item=ApplicationName}";
            eventLogDatabaseTarget.Parameters.Add(param);

            param = new DatabaseParameterInfo();
            param.Name = "@description";
            param.Layout = "${message}";
            eventLogDatabaseTarget.Parameters.Add(param);

            return eventLogDatabaseTarget;
        }