P
Pavan 565
Guest
Hi
Im having a code which logs the data in console window. Instead of that I need to log all my data into local text file.
Here is my code. Please help me to create output text file
public static async Task PostSerialdata()
{
foreach (string serialNumbers in ReadCSVFile())
{
if (!string.IsNullOrEmpty(serialNumbers))
{
string snNumber = serialNumbers.Split(',')[0];
if (!string.IsNullOrEmpty(snNumber))
{
string srNumber = snNumber.Replace('"', ' ').Replace("\r", "").Replace("'", " ").Trim();
if (!isSerialNumberExist)
{
// Request body
var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);
GetToken("https://XXXXXXX/xxxxx").Wait();
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "00000000");
client.DefaultRequestHeaders.Add("Authorization", "Bearer" + " " + token);
var uri = "https://contoso/urisample";
HttpResponseMessage response;
// Request body
//byte[] byteData = Encoding.UTF8.GetBytes(LoadSSJson(srNumber, pnNumber));
byte[] byteData = Encoding.UTF8.GetBytes(LoadJson(srNumber));
using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
response = await client.PostAsync(uri, content);
var responseContent = await response.Content.ReadAsStringAsync();
Thread.Sleep(1000);
}
if (response.StatusCode != HttpStatusCode.OK)
{
#region To Log the Response
Console.WriteLine("Response:");
Console.WriteLine(response.ToString());
if (response.Content != null)
{
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
Console.WriteLine();
#endregion
}
if (response.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("Response:");
Console.WriteLine(response.ToString());
if (response.Content != null)
{
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
Console.WriteLine();
}
}
else if (!string.IsNullOrEmpty(serialNumbers))
{
Console.WriteLine("Serial number: " + snNumber + " already exist in service.");
}
}
else
{
Console.WriteLine("Successfully posted serial numbers to Service System.");
Console.WriteLine("Press any key exit");
Console.ReadLine();
Environment.Exit(0);
}
}
else
{
Console.WriteLine("Response:");
Console.WriteLine("There are Null values in the .csv File");
Console.WriteLine("Please press any Key to Exit");
Console.ReadLine();
Environment.Exit(0);
}
}
}
Continue reading...
Im having a code which logs the data in console window. Instead of that I need to log all my data into local text file.
Here is my code. Please help me to create output text file
public static async Task PostSerialdata()
{
foreach (string serialNumbers in ReadCSVFile())
{
if (!string.IsNullOrEmpty(serialNumbers))
{
string snNumber = serialNumbers.Split(',')[0];
if (!string.IsNullOrEmpty(snNumber))
{
string srNumber = snNumber.Replace('"', ' ').Replace("\r", "").Replace("'", " ").Trim();
if (!isSerialNumberExist)
{
// Request body
var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);
GetToken("https://XXXXXXX/xxxxx").Wait();
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "00000000");
client.DefaultRequestHeaders.Add("Authorization", "Bearer" + " " + token);
var uri = "https://contoso/urisample";
HttpResponseMessage response;
// Request body
//byte[] byteData = Encoding.UTF8.GetBytes(LoadSSJson(srNumber, pnNumber));
byte[] byteData = Encoding.UTF8.GetBytes(LoadJson(srNumber));
using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
response = await client.PostAsync(uri, content);
var responseContent = await response.Content.ReadAsStringAsync();
Thread.Sleep(1000);
}
if (response.StatusCode != HttpStatusCode.OK)
{
#region To Log the Response
Console.WriteLine("Response:");
Console.WriteLine(response.ToString());
if (response.Content != null)
{
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
Console.WriteLine();
#endregion
}
if (response.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("Response:");
Console.WriteLine(response.ToString());
if (response.Content != null)
{
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
Console.WriteLine();
}
}
else if (!string.IsNullOrEmpty(serialNumbers))
{
Console.WriteLine("Serial number: " + snNumber + " already exist in service.");
}
}
else
{
Console.WriteLine("Successfully posted serial numbers to Service System.");
Console.WriteLine("Press any key exit");
Console.ReadLine();
Environment.Exit(0);
}
}
else
{
Console.WriteLine("Response:");
Console.WriteLine("There are Null values in the .csv File");
Console.WriteLine("Please press any Key to Exit");
Console.ReadLine();
Environment.Exit(0);
}
}
}
Continue reading...