本文整理汇总了C#中NLog.Targets.DatabaseTarget.Initialize方法的典型用法代码示例。如果您正苦于以下问题:C# DatabaseTarget.Initialize方法的具体用法?C# DatabaseTarget.Initialize怎么用?C# DatabaseTarget.Initialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NLog.Targets.DatabaseTarget
的用法示例。
在下文中一共展示了DatabaseTarget.Initialize方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SimpleDatabaseTest
public void SimpleDatabaseTest()
{
MockDbConnection.ClearLog();
DatabaseTarget dt = new DatabaseTarget()
{
CommandText = "INSERT INTO FooBar VALUES('${message}')",
ConnectionString = "FooBar",
DBProvider = typeof(MockDbConnection).AssemblyQualifiedName,
};
dt.Initialize(null);
Assert.AreSame(typeof(MockDbConnection), dt.ConnectionType);
List<Exception> exceptions = new List<Exception>();
dt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "msg1").WithContinuation(exceptions.Add));
dt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "msg2").WithContinuation(exceptions.Add));
dt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "MyLogger", "msg3").WithContinuation(exceptions.Add));
foreach (var ex in exceptions)
{
Assert.IsNull(ex, Convert.ToString(ex));
}
string expectedLog = @"Open('FooBar').
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg1')
Close()
Open('FooBar').
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg2')
Close()
Open('FooBar').
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg3')
Close()
";
AssertLog(expectedLog);
}
示例2: GetConnectionString
private string GetConnectionString(DatabaseTarget dt)
{
MockDbConnection.ClearLog();
dt.DBProvider = typeof(MockDbConnection).AssemblyQualifiedName;
dt.CommandText = "NotImportant";
var exceptions = new List<Exception>();
dt.Initialize(null);
dt.WriteAsyncLogEvent(new LogEventInfo(LogLevel.Info, "Logger1", "msg1").WithContinuation(exceptions.Add));
dt.Close();
return MockDbConnection.LastConnectionString;
}
示例3: OdbcShorthandNotationTest
public void OdbcShorthandNotationTest()
{
var dt = new DatabaseTarget()
{
Name = "myTarget",
DBProvider = "odbc",
ConnectionString = "notimportant",
CommandText = "notimportant",
};
dt.Initialize(null);
Assert.Equal(typeof(System.Data.Odbc.OdbcConnection), dt.ConnectionType);
}
示例4: SqlServerShorthandNotationTest
public void SqlServerShorthandNotationTest()
{
foreach (string provName in new[] { "microsoft", "msde", "mssql", "sqlserver" })
{
var dt = new DatabaseTarget()
{
Name = "myTarget",
DBProvider = provName,
ConnectionString = "notimportant",
CommandText = "notimportant",
};
dt.Initialize(null);
Assert.Equal(typeof(System.Data.SqlClient.SqlConnection), dt.ConnectionType);
}
}
示例5: ProviderFactoryInitTest
public void ProviderFactoryInitTest()
{
var dt = new DatabaseTarget();
dt.DBProvider = "MockDb";
dt.CommandText = "Notimportant";
dt.Initialize(null);
Assert.Same(MockDbFactory.Instance, dt.ProviderFactory);
dt.OpenConnection("myConnectionString");
Assert.Equal(1, MockDbConnection2.OpenCount);
Assert.Equal("myConnectionString", MockDbConnection2.LastOpenConnectionString);
}
示例6: ConnectionStringNameNegativeTest
public void ConnectionStringNameNegativeTest()
{
var dt = new DatabaseTarget
{
ConnectionStringName = "MyConnectionString",
CommandText = "notimportant",
ConnectionStringsSettings = new ConnectionStringSettingsCollection(),
};
try
{
dt.Initialize(null);
Assert.True(false, "Exception expected.");
}
catch (NLogConfigurationException configurationException)
{
Assert.Equal("Connection string 'MyConnectionString' is not declared in <connectionStrings /> section.", configurationException.Message);
}
}
示例7: ConnectionStringNameInitTest
public void ConnectionStringNameInitTest()
{
var dt = new DatabaseTarget
{
ConnectionStringName = "MyConnectionString",
CommandText = "notimportant",
};
Assert.Same(ConfigurationManager.ConnectionStrings, dt.ConnectionStringsSettings);
dt.ConnectionStringsSettings = new ConnectionStringSettingsCollection()
{
new ConnectionStringSettings("MyConnectionString", "cs1", "MockDb"),
};
dt.Initialize(null);
Assert.Same(MockDbFactory.Instance, dt.ProviderFactory);
Assert.Equal("cs1", dt.ConnectionString.Render(LogEventInfo.CreateNullEvent()));
}
示例8: DatabaseExceptionTest3
public void DatabaseExceptionTest3()
{
MockDbConnection.ClearLog();
var exceptions = new List<Exception>();
var db = new DatabaseTarget();
db.CommandText = "not important";
db.ConnectionString = "cannotexecute";
db.KeepConnection = true;
db.DBProvider = typeof(MockDbConnection).AssemblyQualifiedName;
db.Initialize(null);
db.WriteAsyncLogEvents(
LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add),
LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add),
LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
db.Close();
Assert.Equal(3, exceptions.Count);
Assert.NotNull(exceptions[0]);
Assert.NotNull(exceptions[1]);
Assert.NotNull(exceptions[2]);
Assert.Equal("Failure during ExecuteNonQuery", exceptions[0].Message);
Assert.Equal("Failure during ExecuteNonQuery", exceptions[1].Message);
Assert.Equal("Failure during ExecuteNonQuery", exceptions[2].Message);
string expectedLog = @"Open('cannotexecute').
ExecuteNonQuery: not important
Close()
Dispose()
Open('cannotexecute').
ExecuteNonQuery: not important
Close()
Dispose()
Open('cannotexecute').
ExecuteNonQuery: not important
Close()
Dispose()
";
AssertLog(expectedLog);
}
示例9: DatabaseExceptionTest1
public void DatabaseExceptionTest1()
{
MockDbConnection.ClearLog();
var exceptions = new List<Exception>();
var db = new DatabaseTarget();
db.CommandText = "not important";
db.ConnectionString = "cannotconnect";
db.DBProvider = typeof(MockDbConnection).AssemblyQualifiedName;
db.Initialize(null);
db.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
db.Close();
Assert.Equal(1, exceptions.Count);
Assert.NotNull(exceptions[0]);
Assert.Equal("Cannot open fake database.", exceptions[0].Message);
Assert.Equal("Open('cannotconnect').\r\n", MockDbConnection.Log);
}
示例10: ParameterFacetTest
public void ParameterFacetTest()
{
MockDbConnection.ClearLog();
DatabaseTarget dt = new DatabaseTarget()
{
CommandText = "INSERT INTO FooBar VALUES(@msg, @lvl, @lg)",
DBProvider = typeof(MockDbConnection).AssemblyQualifiedName,
KeepConnection = true,
Parameters =
{
new DatabaseParameterInfo("msg", "${message}")
{
Precision = 3,
Scale = 7,
Size = 9,
},
new DatabaseParameterInfo("lvl", "${level}")
{
Scale = 7
},
new DatabaseParameterInfo("lg", "${logger}")
{
Precision = 0
},
}
};
dt.Initialize(null);
Assert.Same(typeof(MockDbConnection), dt.ConnectionType);
// when we pass multiple log events in an array, the target will bucket-sort them by
// connection string and group all commands for the same connection string together
// to minimize number of db open/close operations
// in this case msg1, msg2 and msg4 will be written together to MyLogger database
// and msg3 will be written to MyLogger2 database
var exceptions = new List<Exception>();
var events = new[]
{
new LogEventInfo(LogLevel.Info, "MyLogger", "msg1").WithContinuation(exceptions.Add),
new LogEventInfo(LogLevel.Debug, "MyLogger2", "msg3").WithContinuation(exceptions.Add),
};
dt.WriteAsyncLogEvents(events);
dt.Close();
foreach (var ex in exceptions)
{
Assert.Null(ex);
}
string expectedLog = @"Open('Server=.;Trusted_Connection=SSPI;').
CreateParameter(0)
Parameter #0 Direction=Input
Parameter #0 Name=msg
Parameter #0 Size=9
Parameter #0 Precision=3
Parameter #0 Scale=7
Parameter #0 Value=msg1
Add Parameter Parameter #0
CreateParameter(1)
Parameter #1 Direction=Input
Parameter #1 Name=lvl
Parameter #1 Scale=7
Parameter #1 Value=Info
Add Parameter Parameter #1
CreateParameter(2)
Parameter #2 Direction=Input
Parameter #2 Name=lg
Parameter #2 Value=MyLogger
Add Parameter Parameter #2
ExecuteNonQuery: INSERT INTO FooBar VALUES(@msg, @lvl, @lg)
CreateParameter(0)
Parameter #0 Direction=Input
Parameter #0 Name=msg
Parameter #0 Size=9
Parameter #0 Precision=3
Parameter #0 Scale=7
Parameter #0 Value=msg3
Add Parameter Parameter #0
CreateParameter(1)
Parameter #1 Direction=Input
Parameter #1 Name=lvl
Parameter #1 Scale=7
Parameter #1 Value=Debug
Add Parameter Parameter #1
CreateParameter(2)
Parameter #2 Direction=Input
Parameter #2 Name=lg
Parameter #2 Value=MyLogger2
Add Parameter Parameter #2
ExecuteNonQuery: INSERT INTO FooBar VALUES(@msg, @lvl, @lg)
Close()
Dispose()
";
AssertLog(expectedLog);
}
示例11: KeepConnectionOpenBatchedTest2
public void KeepConnectionOpenBatchedTest2()
{
MockDbConnection.ClearLog();
DatabaseTarget dt = new DatabaseTarget()
{
CommandText = "INSERT INTO FooBar VALUES('${message}')",
ConnectionString = "Database=${logger}",
DBProvider = typeof(MockDbConnection).AssemblyQualifiedName,
KeepConnection = true,
};
dt.Initialize(null);
Assert.Same(typeof(MockDbConnection), dt.ConnectionType);
// when we pass multiple log events in an array, the target will bucket-sort them by
// connection string and group all commands for the same connection string together
// to minimize number of db open/close operations
// in this case msg1, msg2 and msg4 will be written together to MyLogger database
// and msg3 will be written to MyLogger2 database
List<Exception> exceptions = new List<Exception>();
var events = new[]
{
new LogEventInfo(LogLevel.Info, "MyLogger", "msg1").WithContinuation(exceptions.Add),
new LogEventInfo(LogLevel.Info, "MyLogger", "msg2").WithContinuation(exceptions.Add),
new LogEventInfo(LogLevel.Info, "MyLogger2", "msg3").WithContinuation(exceptions.Add),
new LogEventInfo(LogLevel.Info, "MyLogger", "msg4").WithContinuation(exceptions.Add),
};
dt.WriteAsyncLogEvents(events);
foreach (var ex in exceptions)
{
Assert.Null(ex);
}
string expectedLog = @"Open('Database=MyLogger').
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg1')
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg2')
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg4')
Close()
Dispose()
Open('Database=MyLogger2').
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg3')
";
AssertLog(expectedLog);
MockDbConnection.ClearLog();
dt.Close();
expectedLog = @"Close()
Dispose()
";
AssertLog(expectedLog);
}
示例12: KeepConnectionOpenBatchedTest
public void KeepConnectionOpenBatchedTest()
{
MockDbConnection.ClearLog();
DatabaseTarget dt = new DatabaseTarget()
{
CommandText = "INSERT INTO FooBar VALUES('${message}')",
ConnectionString = "FooBar",
DBProvider = typeof(MockDbConnection).AssemblyQualifiedName,
KeepConnection = true,
};
dt.Initialize(null);
Assert.Same(typeof(MockDbConnection), dt.ConnectionType);
var exceptions = new List<Exception>();
var events = new[]
{
new LogEventInfo(LogLevel.Info, "MyLogger", "msg1").WithContinuation(exceptions.Add),
new LogEventInfo(LogLevel.Info, "MyLogger", "msg2").WithContinuation(exceptions.Add),
new LogEventInfo(LogLevel.Info, "MyLogger", "msg3").WithContinuation(exceptions.Add),
};
dt.WriteAsyncLogEvents(events);
foreach (var ex in exceptions)
{
Assert.Null(ex);
}
string expectedLog = @"Open('FooBar').
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg1')
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg2')
ExecuteNonQuery: INSERT INTO FooBar VALUES('msg3')
";
AssertLog(expectedLog);
MockDbConnection.ClearLog();
dt.Close();
expectedLog = @"Close()
Dispose()
";
AssertLog(expectedLog);
}
示例13: DontRequireProviderNameInAppConfig
public void DontRequireProviderNameInAppConfig()
{
LogManager.ThrowExceptions = true;
var databaseTarget = new DatabaseTarget()
{
Name = "myTarget",
ConnectionStringName = "test_connectionstring_without_providerName",
CommandText = "notimportant",
DBProvider = "System.Data.SqlClient"
};
databaseTarget.ConnectionStringsSettings = new ConnectionStringSettingsCollection()
{
new ConnectionStringSettings("test_connectionstring_without_providerName", "some connectionstring"),
new ConnectionStringSettings("test_connectionstring_with_providerName", "some connectionstring",
"System.Data.SqlClient"),
};
databaseTarget.Initialize(null);
Assert.NotNull(databaseTarget.ProviderFactory);
Assert.Equal(typeof(System.Data.SqlClient.SqlClientFactory), databaseTarget.ProviderFactory.GetType());
}