I use the log4net code in app.connfig file but then the service wouldn't start. If I use a code as a seperate class and call it in the constructor of the service it does. If I do it in the latter way, the logs aren't being logged.
My config file code is :-
<?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:\log\impersonationlog_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>
If I use this class instead of the app.config code, the service starts but the logging doesn't happen always.
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;
}
}
The app.config code was running fine till yesterday afternoon after some additions to the module it is behaving this way. This method in the class is called in the constructor as shown below :-
public Service1()
{
InitializeComponent();
Logger.Setup();
}
Please help me overcome this and log the data properly again.
The changes made was I added this code and called this function within another function that starts off the whole service's default working:-
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
}
}
As for what you have written in you app.config
file i can assume that you haven't added the configSections
with a new section named as Log4net
.
So, what you can do is make the following changes to make the Log4net
work.
1st app.config
:
Make changes from this
<?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:\log\impersonationlog_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>
to this:
<?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:\log\impersonationlog_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>
Then in the class
in which you want to use create an global
object of the Log4net
as:
ILog log = LogManager.GetLogger(typeof(yourclassname));
and then in the class just use it like:
log.info("hi i am a log");
Hope this helps.