使用log4net的例子:
http://sadi02.wordpress.com/2008/06/29/log4net-tutorial-in-c-net-how-can-i-show-log-in-a-file/
我正在使用命令输入参数'User'或'NoUser'。 用户将仅将文本输出到控制台,而NoUser将仅将文本输出到日志文件。从示例中我得到了它,但是当输入'NoUser'时,它会将文本写入输出文件AND控制台应用程序。
所以我使用静态类来设置User / No user:
public static class Log
{
public static readonly ILog log = LogManager.GetLogger(typeof(Program));
public static bool _user;
public static void UseLog4Net(string msg)
{
if (_user)
{
Console.WriteLine(msg);
}
else
{
log.Debug(msg);
}
}
}
然后从程序中我尝试测试:
assembly: log4net.Config.XmlConfigurator(Watch = true)]
static void Main(string[] args)
{
bool verbose = false;
bool user = false;
try
{
OutputHeader();
ProcessParameters(args, out verbose, out user);
Log._verbose = verbose;
Log._user = user;
}
}
private static void ProcessParameters(string[] args, out bool verbose, out bool user)
{
verbose = CheckArgs(args, "verbose", "quiet");
user = CheckArgs(args, "user", "nouser");
if (!user)
{
Console.WriteLine("TEST 1");
//Console.SetOut(File.CreateText(@".\log" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".log"));
log4net.Config.BasicConfigurator.Configure();
Log.UseLog4Net("ABC");
Console.ReadLine();
OutputHeader();
}
}
private static bool CheckArgs(string[] args, string trueValue, string falseValue)
{
foreach (string arg in args)
{
if (arg.ToLower() == trueValue.ToLower())
{
return true;
}
else if (arg.ToLower() == falseValue.ToLower())
{
return false;
}
}
return false;
}
这里ABC被写入控制台和文本文件。我只想将它写入文本文件。
有什么想法吗?
更新
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="C:\Try\logger\logger\bin\Debug\log + "Monday" + .txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
</layout>
</appender>
</log4net>