我在app.connfig文件中使用了log4net代码,但随后服务无法启动。如果我将代码用作单独的类并在服务的构造函数中调用它,它会这样做。如果我以后一种方式执行此操作,则不会记录日志。
我的配置文件代码是:-
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<log4net>
<appender name="TestAppender" type="log4net.Appender.RollingFileAppender" >
<file value="C:logimpersonationlog_JamochaService.log" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<!--<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="5MB" />
<staticLogFileName value="true" />-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level [%thread] %type.%method - %message%n" />
</layout>
</appender>
<root>
<level value="All" />
<!-- If the following line is not included the log file
will not be created even if log4net is configured with this file. -->
<appender-ref ref="TestAppender" />
</root>
</log4net>
</configuration>
如果我使用此类而不是 app.config 代码,服务将启动,但日志记录并不总是发生。
public class Logger
{
public static void Setup()
{
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
patternLayout.ActivateOptions();
RollingFileAppender roller = new RollingFileAppender();
roller.AppendToFile = false;
roller.File = "C:\Logs\EventLog.txt";
roller.Layout = patternLayout;
roller.MaxSizeRollBackups = 5;
roller.MaximumFileSize = "1GB";
roller.RollingStyle = RollingFileAppender.RollingMode.Size;
roller.StaticLogFileName = true;
roller.ActivateOptions();
hierarchy.Root.AddAppender(roller);
MemoryAppender memory = new MemoryAppender();
memory.ActivateOptions();
hierarchy.Root.AddAppender(memory);
hierarchy.Root.Level = Level.Info;
hierarchy.Configured = true;
}
}
app.config 代码在对模块进行一些添加后直到昨天下午运行良好,它的行为方式是这样的。类中的此方法在构造函数中调用,如下所示:-
public Service1()
{
InitializeComponent();
Logger.Setup();
}
请帮助我克服此问题并再次正确记录数据。
所做的更改是我添加了此代码并在另一个函数中调用此函数,该函数启动了整个服务的默认工作:
private void SocketCommunication()
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
permission = new SocketPermission(NetworkAccess.Accept,
TransportType.Tcp, "", SocketPermission.AllPorts);
IPHostEntry ipHost = Dns.GetHostEntry("");
IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 4510);
socket.Bind(ipEndPoint); //Bind to the client's IP
socket.Listen(10);//Listen for maximum 10 connections
sListener = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
Log.Info("Waiting for a client...");
client = socket.Accept();
IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
Log.Info("Connected with " + clientep.Address + " at port " + clientep.Port);
while (true)
{
//string welcome = "Welcome. "; //This is the data we we'll respond with
//byte[] data = new byte[1024];
//data = Encoding.ASCII.GetBytes(welcome); //Encode the data
//client.Send(data, data.Length, SocketFlags.None); //Send the data to the client
//Console.WriteLine("Disconnected from {0}", clientep.Address);
byte[] data = new byte[1024];
int receivedDataLength = client.Receive(data);
string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
Log.Info("Data recieved :- " + stringData);
//Decode the data received
int adder = Convert.ToInt32(stringData);
adder++;
string input = adder.ToString();
data = Encoding.ASCII.GetBytes(input);
client.Send(data, data.Length, SocketFlags.None);
Log.Info("Data sent :- " + input); //data sent
}
}
至于您在app.config
文件中写的内容,我可以假设您没有添加名为Log4net
的新部分的configSections
。 因此,您可以做的是进行以下更改以使Log4net
正常工作。
第一app.config
:
从此进行更改
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<log4net>
<appender name="TestAppender" type="log4net.Appender.RollingFileAppender" >
<file value="C:logimpersonationlog_JamochaService.log" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<!--<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="5MB" />
<staticLogFileName value="true" />-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level [%thread] %type.%method -
%message%n" />
</layout>
</appender>
<root>
<level value="All" />
<!-- If the following line is not included the log file
will not be created even if log4net is configured with this file. -->
<appender-ref ref="TestAppender" />
</root>
</log4net>
</configuration>
对此:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"></section>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<log4net>
<appender name="TestAppender" type="log4net.Appender.RollingFileAppender" >
<file value="C:logimpersonationlog_JamochaService.log" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<!--<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="5MB" />
<staticLogFileName value="true" />-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level [%thread] %type.%method -
%message%n" />
</layout>
</appender>
<root>
<level value="All" />
<!-- If the following line is not included the log file
will not be created even if log4net is configured with this file. -->
<appender-ref ref="TestAppender" />
</root>
</log4net>
</configuration>
然后在要使用的class
中创建Log4net
的global
对象,如下所示:
ILog log = LogManager.GetLogger(typeof(yourclassname));
然后在课堂上使用它,就像:
log.info("hi i am a log");
希望这有帮助。