Create an amsx web service with log file ? RRS feed

  • Question

  • User801422497 posted


    can anyone suggest me how to build log files for an asmx web service. i have made the webservice and now i want to log the errors in log file if any occured during execution of web service.

    any help will be great..!!


    Thursday, September 19, 2013 12:12 PM


All replies

  • User-1662538993 posted

    When you would like to add you can use below code -


    You can do somehting like this -

    string strLogText = "Some details you want to log.";
    // Create a writer and open the file:
    StreamWriter log;
    if (!File.Exists("logfile.txt"))
      log = new StreamWriter("logfile.txt");
      log = File.AppendText("logfile.txt");
    // Write to the file:
    // Close the stream:

    Thursday, September 19, 2013 12:41 PM
  • User801422497 posted

    Thanks for quick response kushalrDalal, but i want to add something that i want to do this using log4net.dll

    i have gone through - http://social.msdn.microsoft.com/Forums/vstudio/en-US/7db558e7-ccc0-46f4-b74e-35bd633e014c/use-log4net-in-a-dll-project

    but the file isn't coming up in my local hard drive as i specify it in my web.config file.

    -- in web.config file

    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
    <param name="File" value="C:\MyLogg.txt" />
    <param name="AppendToFile" value="true" />
    <layout type="log4net.Layout.PatternLayout">
    <param name="Header" value="" />
    <param name="Footer" value="" />
    <param name="ConversionPattern" value="%d [%t] %-5p %m%n" />
    <!--LogLevel: OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
    <level value="ALL" />
    <appender-ref ref="LogFileAppender" />

    and in .cs file i have used log4net dll : 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;

    using log4net;
    using log4net.Config;
    using System.ComponentModel;
    using System.IO;

    namespace WebService1
    public enum LogLevelL4N
    DEBUG = 1,

    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    private static readonly ILog aLog =LogManager.GetLogger(typeof(Service1));

    public Service1()
    aLog.Info("Service Initialized !");

    #region Component Designer generated code

    //Required by the Web Services Designer
    private IContainer components = null;

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose(bool disposing)
    if (disposing && components != null)


    public int HelloWorld()
    int a =1;
    int b = 0;
    return a/b;
    catch(Exception ex)
    aLog.Info("Error in Service: " + ex.Message);
    return 0;


    but im not having my ohysical file created in C drive.!1

    any help ?? 


    Thursday, September 19, 2013 2:06 PM
  • User260886948 posted


    Please make sure you are calling the Configure function of the XmlConfigurator as below:

    #log4Net log file not writing:
    http://stackoverflow.com/questions/13525261/log4net-log-file-not-writing .

    #Log4Net Webservice not creating file:
    http://stackoverflow.com/questions/9412883/log4net-webservice-not-creating-file-what-am-i-doing-wrong .

    Best Regards.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 20, 2013 1:47 AM
  • User-488622176 posted

    Log4Net will do the trick. See this article for complete code : http://orkunsurucuoglu.blogspot.be/2013/03/sample-log4net-implementation-in-wcf.html

    Beware of logging to files : services are multithreaded, files can be locked and causing the logging process to fail.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 20, 2013 9:46 AM
  • User801422497 posted

    Thanks you guys, your suggestions were really helpful. The service class was missing the service reference and there were other minor issues and the links provided were really useful.

    My service class is creating a log file in my local machine and the server where it should be. 



    Saturday, September 21, 2013 12:53 AM