Skip to content

Instantly share code, notes, and snippets.

@professionalsna
Last active October 8, 2020 07:33
Show Gist options
  • Save professionalsna/3446d54883338d2ede13c1a0540246e8 to your computer and use it in GitHub Desktop.
Save professionalsna/3446d54883338d2ede13c1a0540246e8 to your computer and use it in GitHub Desktop.
Log Response to File TXT, XML or Json using C# Helper
public enum NavFileTypes {
TXT,
JSON,
XML
}
class NavUtilities
{
public static void LogResponseToFile(string content ,string fileName, NavFileTypes fileTypes)
{
if (!Directory.Exists(@"c:\Log"))
{
Directory.CreateDirectory(@"c:\Log");
}
var fname = fileName+"_" + DateTime.Now.ToString("ddMMyyyyHHmmss");
string filePath;
switch (fileTypes)
{
case NavFileTypes.TXT:
filePath = @"c:\Log\" + fname + ".txt";
break;
case NavFileTypes.JSON:
filePath = @"c:\Log\" + fname + ".json";
break;
case NavFileTypes.XML:
filePath = @"c:\Log\" + fname + ".xml";
break;
default:
filePath = @"c:\Log\" + fname + ".txt";
break;
}
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine(content);
sw.Flush();
sw.Close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment