Write to a log file in ASP.NET

前端 未结 3 2046
别跟我提以往
别跟我提以往 2020-12-31 17:47

I am writing event data to a log file in an asp.net httphandler by using the File.AppendAllText method. I am concerned with what will happen when multiple requests are rece

3条回答
  •  天涯浪人
    2020-12-31 18:26

    I recommend using the TextWriterTraceListener instead of trying to manage this your self.

    It is very simple to setup and use:

    TextWriterTraceListener logListener = new TextWriterTraceListener("C:\log.txt", "My Log Name");
    Trace.Listeners.Add(logListener);
    

    And then to log something:

    Trace.WriteLine("Log this text");
    

    It is very simple to use and also there are many different types of listeners for SQL, Event Log, text file, etc. So you won't have to adjust your code if you want to change out the listener.

提交回复
热议问题