c# - static class with so many request at same time -


i created static ip , log class. ip class find out users ip address , log class log text file. every thing work fine wonder happens if many requests came @ same time? mean both classes static , base on static classes causes problem. how can managed them? here ip class:

    public static class ip     {         public static string ip()         {             system.web.httpcontext context = system.web.httpcontext.current;              string ipaddress = context.request.servervariables["http_x_forwarded_for"];              if (!string.isnullorempty(ipaddress))             {                 string[] addresses = ipaddress.split(',');                 if (addresses.length != 0)                 {                     return addresses[0];                 }             }              return context.request.servervariables["remote_addr"];         }     } } 

and here part of log class write text file:

        private static void writeline(string message)     {         string filepath = filepath();         createfile(filepath);         try         {             using (streamwriter log = file.appendtext(filepath))                 log.writeline(message);         }         catch (exception)         {             //if can not access file nothing             //throw;         }     } 

you aren't going run contention problems due classes being static. ip.ip() method class pure (i.e. not change state of anything) , contains no locks, there no chance of there being contention there.

you potentially have problems in writeline due fact writing log file on same thread doing work. means file write acting lock since 1 write can occur @ 1 time.

what want log queue , write queue on separate thread; classic producer-consumer pattern.

alternatively avoid reinventing wheel , use existing logging framework handle these things log4net


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -