c# - Limit lines of a txt file -


i have text file save line every 30 minutes writelineasync method. when file become large, if try read it, application crashes. thought limit lines writable on file when add new line, oldest line deleted. how can that?

edit: read file following code:

storagefile myfile = await applicationdata.current.localfolder.getfileasync("logfile.txt"); string nextline; using (streamreader reader = new streamreader(await myfile.openstreamforreadasync())) {     while ((nextline = await reader.readlineasync()) != null)     {         textlog.text += nextline + "\n";     } } 

i've tried debug , don't exception reading code. maybe problem try put text in textblock.

if need text file have 1 of 2 following:

  1. read entire file memory, chop off unwanted part, , write out
  2. read file line line, ignoring first x lines below threshold, writing out lines want temporary file. once you've filtered lines want, move temporary file on top of existing file.

there other alternatives:

  • use file format / data structure supports arbitrary deletes, database.
  • use rolling logs, starting new file when existing 1 becomes big. there plenty of existing logging libraries out of box bit of configuration.
  • stop reading whole file, read end of it. granted, has both upsides , downsides. upside can keep file big want. downside if need guarantee read last n lines takes bit more work ensure that.

Comments