How to write times on console entries without repeating too much code

Asked

Viewed 97 times

0

I wish that all Console.WriteLine this way when sent to the Console:

[10:38:12:758]- Message typed in writeline

Well I’ve already done a part that’s this:

private static DateTime data = DateTime.Now;
Console.WriteLine("[{0}]", data.ToString("HH:mm:ss:fff"));

However, I want to write several messages, and in case I would have to keep typing this code a lot of times, I wanted it to be something like this:

"CW("Message")" and already appeared the "Datetime now" in the format I quoted above, how can I do this?

I made this code:

public void msg(string mensagem)
{
    mensagem = "";
    Console.WriteLine("[{0}], [{1}]", data.ToString("HH:mm:ss:fff"), mensagem);
}

private void button1_Click(object sender, EventArgs e)
{
    msg("Teste");
}

However, the message "Test"

  • Wouldn’t it be better to put all the code?

  • I edited and put the code

  • This code makes no sense.

  • Yeah, I put something that didn’t make sense there haha

  • 1

    You’re leaving the test variable without text there mensagem = "";

  • Do you want to write a message and add the date together? just don’t want to keep typing the date into every message ?

  • I’ve got a friend thanks.

Show 2 more comments

3 answers

6


Create your own method that receives text as input ;)

public void EscreverNoConsole(string texto) {
    Console.WriteLine("[{0}]-{1}", data.ToString("HH:mm:ss:fff"), texto);
}

Hence, whenever you want to write on the console, instead of calling Console.Writeline, call the method you created.

EscreverNoConsole("Quero vê-la sorrir, quero vê-la cantar");
EscreverNoConsole("Quero ver o seu corpo dançar sem parar");

This is called encapsulation. Everything you need to repeat a lot, you can encapsulate in a function or method.

  • 1

    +1 by Sidney Magal

3

You can do this (each line in a very different place, it’s not a sequential code):

using static System.Console;

private static DateTime data = DateTime.Now;

WriteLine($"[{data.ToString("HH:mm:ss:fff")}]");

I wouldn’t, but if I wanted to, I would:

public static void WL(string format, params args) => WriteLine(format, string[] args);

According to the issue I would do so:

public void CW(string mensagem) => WriteLine($"[{data.ToString("HH:mm:ss:fff")}]-{mensagem}");

I put in the Github for future reference.

Hard to be "simplify" more than this.

1

Create a method that receives as parameter the message you want to print:

public void ExibirMensagem(string msg)
{
    Console.WriteLine("[{0}] - {1}", DateTime.Now.ToString("HH:mm:ss:fff"), msg);
}

So you call the method by passing the text that should be shown and it will be displayed in the specified formatting.

example:

if (1)
    ExibirMensagem("Mensagem número UM");
else if (2)
    ExibirMensagem("Mensagem número DOIS");
else
    ExibirMensagem("Mensagem número TRÊS");

return:

[10:47:12:756]- Mensagem número UM
[10:47:12:757]- Mensagem número DOIS
[10:47:12:758]- Mensagem número TRÊS
  • Thanks, I just made a mistake on a simple little thing kkkk

  • 1

    This code does not even compile

  • Vlw, @Maniero.. corrected

Browser other questions tagged

You are not signed in. Login or sign up in order to post.