[英]How to setup NLOG to use multiple database
In MVC project, have 5 separate areas.在 MVC 项目中,有 5 个独立的区域。 and other two separate projects in same solution.以及同一解决方案中的其他两个独立项目。
there needs to have, three MVC areas to use NLOG with one database and other two areas to use NLOG with other database.需要有,三个 MVC 区域将 NLOG 与一个数据库一起使用,另外两个区域将 NLOG 与其他数据库一起使用。 Also, other two projects in same solution those also needs to use NLOG with different database.此外,同一解决方案中的其他两个项目也需要使用不同数据库的 NLOG。
So, as a solution, I am going to create separate project of NLOG in SLN and needs to setup separate NLOG project to allow different database to log based on request coming from related MVC areas or project.因此,作为解决方案,我将在 SLN 中创建单独的 NLOG 项目,并且需要设置单独的 NLOG 项目以允许不同的数据库根据来自相关 MVC 区域或项目的请求进行记录。
How to accomplish this ?如何做到这一点?
Thanks谢谢
It is possible with NLogs default behaviour. NLogs 默认行为是可能的。 We have to take quick a look at NLog:我们必须快速浏览一下 NLog:
NLog defines Loggers
by Names. NLog 按名称定义Loggers
。 If a Logger
has to write a log-message, NLog checks which rules should be applied.如果Logger
必须写入日志消息,NLog 会检查应应用哪些规则。
For every log-message
all rules are checked.对于每条log-message
都会检查所有规则。 If the Logger
-Name and the minimum LogLevel
is matching, the log-message
will be written to the Target
.如果Logger
-Name 和最小LogLevel
匹配,则log-message
将写入Target
。
Targets
can be Log-Files, Databases or something else where the log-messages
are persisted. Targets
可以是日志文件、数据库或其他保存log-messages
东西。
Typically you create your Logger
by requesting the DefaultClassLogger
from the LogManager
.通常,您通过从LogManager
请求DefaultClassLogger
来创建Logger
。 This will create a Logger
with the name "Namespace.SubnameSpace.ClassName".这将创建一个名为“Namespace.SubnameSpace.ClassName”的Logger
。 Alternatively you can request a Logger
with a customname by calling LogManager.GetLogger("YourLoggerName")
.或者,您可以通过调用LogManager.GetLogger("YourLoggerName")
来请求具有自定义名称的Logger
。
If you want to log to two Targets
at the same LogLevel
you have to use two Loggers
with different names.如果要在同一LogLevel
上登录到两个Targets
,则必须使用两个具有不同名称的Loggers
。 Those are only defined within you rules.这些仅在您的规则中定义。
Loggers
from the LogManager
从LogManager
请求正确的Loggers
<?xml version="1.0" encoding="utf-8"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="logfile1" xsi:type="File" fileName="file1.txt" />
<target name="logfile2" xsi:type="File" fileName="file2.txt" />
<target name="logfileAllLogs" xsi:type="File" fileName="fileAllLogs.txt" />
</targets>
<rules>
<logger name="Logger1" minlevel="Info" writeTo="logfile1" />
<logger name="Logger2" minlevel="Info" writeTo="logfile2" />
<logger name="*" minlevel="Info" writeTo="logfileAllLogs" /> <!-- Will be asked for all names because of the wildcard '*' -->
</rules>
</nlog>
// Create your targets
FileTarget fileTarget1 = new FileTarget() { FileName = "file1.txt", Name = "logfile1" };
FileTarget fileTarget2 = new FileTarget() { FileName = "file2.txt", Name = "logfile2" };
FileTarget fileTargetAll = new FileTarget() { FileName = "fileAll.txt", Name = "logfileAll" };
// Create your rules
LoggingRule rule1 = new LoggingRule("Logger1", LogLevel.Info, fileTarget1); // This will be used only Logged to Loggers with the name "Logger1". Attention: Case-sensitive!
LoggingRule rule2 = new LoggingRule("Logger2", LogLevel.Info, fileTarget2);
LoggingRule ruleAll = new LoggingRule("*", LogLevel.Info, fileTargetAll); // This will be used by all existing loggers, because of the wildcard "*"
// create a configuration to introduce the rules and targets to the LogManager.
LoggingConfiguration config = new LoggingConfiguration();
// Add targets to the config
config.AddTarget(fileTarget1.Name, fileTarget1); // don't know why i have to give the name separately
config.AddTarget(fileTarget2.Name, fileTarget2);
config.AddTarget(fileTargetAll.Name, fileTargetAll);
// Add rules to the config
config.LoggingRules.Add(rule1);
config.LoggingRules.Add(rule2);
config.LoggingRules.Add(ruleAll);
// Set the config as current config
LogManager.Configuration = config;
// Let's log.. Get your loggers..
Logger logger1 = LogManager.GetLogger("Logger1");
Logger logger2 = LogManager.GetLogger("Logger2");
// Log something..
logger1.Error("Test Logger1a");
logger2.Error("Test Logger2a");
logger1.Error("Test Logger1b");
logger2.Error("Test Logger2b");
Logger logger1 = LogManager.GetLogger("Logger1");
Logger logger2 = LogManager.GetLogger("Logger2");
logger1.Error("Test Logger1a");
logger2.Error("Test Logger2a");
logger1.Error("Test Logger1b");
logger2.Error("Test Logger2b");
// ---------- file1.txt ----------
// Test Logger1a
// Test Logger1b
// -------------------------------
//
// ---------- file2.txt ----------
// Test Logger2a
// Test Logger2b
// -------------------------------
//
// ---------- fileAll.txt ----------
// Test Logger1a
// Test Logger2a
// Test Logger1b
// Test Logger2b
// -------------------------------
In your example you propably got two DLLs with different namespaces.在您的示例中,您可能有两个具有不同名称空间的 DLL。 Something like Db1Lib.MyClass
and Db2Lib.MyClass
.像Db1Lib.MyClass
和Db2Lib.MyClass
类的东西。 If you want to set up your Loggers to log each DLL into an dedicated db, you should name your first Logger "Db1Lib*" and you second one "Db2Lib*".如果您想设置您的 Logger 以将每个 DLL 记录到一个专用数据库中,您应该将您的第一个 Logger 命名为“Db1Lib*”,将第二个命名为“Db2Lib*”。 This way LogManager.GetCurrentClassLogger()
will Name the Loggers correct.这样LogManager.GetCurrentClassLogger()
将正确命名记录器。 But this only works if your dbs are in different namespaces/classes.但这仅在您的数据库位于不同的命名空间/类中时才有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.