[英]working with log4net package
I recently added the log4net package in to my WCF web application. 我最近将log4net软件包添加到了WCF Web应用程序中。 now I have few questions I:
现在我有几个问题:
I added the log4net package through the VS 2013 package install and it added whole package. 我通过VS 2013软件包安装添加了log4net软件包,并添加了整个软件包。 My guess is that log4net is just the dll and I can add it to my project just by adding the dll?
我的猜测是log4net只是dll,我可以通过添加dll将其添加到我的项目中吗?
when I added log4net package a packages.config file has been added to my project with this content: 当我添加log4net软件包时,一个packages.config文件已添加到我的项目中,其内容如下:
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="log4net" version="2.0.3" targetFramework="net45" /> </packages>
what is it? 它是什么? can I remove it?
我可以删除它吗?
Can I use the log4net all over my project without defining it in top of each class? 我可以在整个项目中使用log4net而不在每个类的顶部定义它吗?
I want to add a extra field to message part. 我想在消息部分添加一个额外的字段。 for example I want to log the events like this:
例如,我想记录这样的事件:
Log.Debug(IP,"This is a DEBUG level message. Typically your most VERBOSE level."); Log.Debug(IP,“这是一个调试级别的消息。通常是最详细的级别。”);
and the log file show it like this: 日志文件显示如下:
72 DEBUG 2015-06-16 08:17:41,713 [10] [(null)] [InsertDate] - from source IP(192.168.1.1) This is a DEBUG level message. Typically your most VERBOSE level.
I also added %line
in to conversionPattern but it doesn't work. 我也将
%line
添加到conversionPattern中,但是它不起作用。 every time it start from 72. How can this be fixed? 每次从72开始。如何解决?
How can log the application stop and start in the wcf application? 如何在WCF应用程序中记录应用程序的停止和启动?
You actually asked 4 questions in 1. First, you need to understand that you have used a NuGet package manager and it is a good practice to include available packages using NuGet but not manually as it automatically places package in a proper place, lets you add this package easily to other projects of solution, provides versioning etc. Read more about NuGet. 您实际上在1中提出了4个问题。首先,您需要了解您已经使用了NuGet软件包管理器,这是一个很好的做法,可以使用NuGet包含可用软件包,但不要手动添加,因为它会自动将软件包放置在适当的位置,您可以添加该软件包可以轻松地解决方案的其他项目,提供版本控制等。有关NuGet的更多信息。 For example, here .
例如, 这里 。
Now about your questions: 现在关于您的问题:
First : No, log4net
package contains not only a log4net.dll
file. 首先 :不,
log4net
软件包不仅包含log4net.dll
文件。 It also has log4net.xml
file. 它还具有
log4net.xml
文件。 Yes, you can add these two files manually without using NuGet package manager. 是的,您可以手动添加这两个文件,而无需使用NuGet软件包管理器。
Second : It is a list of NuGet packages in your project. 第二 :这是项目中NuGet软件包的列表。 You can only remove it if you are not going to use NuGet.
如果您不打算使用NuGet,则只能将其删除。 However, it is not a good idea - let it be there, it is a single light-weight XML-file.
但是,这不是一个好主意-放在那里,它是一个轻量级的XML文件。
Third : It's a question of OOP. 第三 :这是面向对象的问题。 Create a singleton global logger, for example:
创建一个单例全局记录器,例如:
public static class LoggingFactory
{
private static ILog _logger;
private static ILog CreateLogger()
{
// Some log4net initialization
return LogManager.GetLogger("Logger");
}
public static ILog GetLogger()
{
return _logger ?? (_logger = CreateLogger());
}
}
public class AnyClassOfYourWholeSolution
{
LoggingFactory.GetLogger().DebugFormat("{0} {1}", a, b);
}
Fourth: It is a question of OOP again. 第四:这又是一个面向对象的问题。 Create an adapter and use it:
创建一个适配器并使用它:
public interface ILogger
{
void Debug(string ip, string message);
void Info(string ip, string message);
void Error(string ip, string message);
}
public class Log4NetLogger : ILogger
{
private ILog _logger;
public Log4NetLogger()
{
// Some log4net initialization
_logger = LogManager.GetLogger("Logger");
}
public void Debug(string ip, string message)
{
// ...
}
public void Info(string ip, string message)
{
// ...
}
public void Error(string ip, string message)
{
// ...
}
}
public static class LoggingFactory
{
private static ILogger _loggerAdapter;
private static Initialize(ILogger adapter)
{
_loggerAdapter = adapter;
}
public static GetLogger()
{
return _logger;
}
}
public class BeginningOfYourProject
{
// Main() or Global.asax or Program.cs etc.
LoggingFactory.Initialize(new Log4NetLogger());
}
public class AnyClassOfYourWholeProject
{
LoggingFactory.GetLogger().Debug(ip, message);
}
Or you can extract ip to the properties of log4net: 或者您可以将ip提取到log4net的属性中:
// During initialization
log4net.ThreadContext.Properties["ip"] = ip;
// log4net Config
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%n%-5p %d %t IP: %property{ip} %m" />
</layout>
It is all up to your fantasy. 这完全取决于您的幻想。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.