0
Eai guys, I’m trying to generate Logs for a web application. ASP, however I cannot find the correct C# method to find the Line and File where the error is occurring. Within the exception I found the Stacktrace method but it brings all this information together, I wonder if someone can help me generate a log that brings the File, Method and Error Line separately and "cute".
I thank you and we are together!!!
PS: follows below my code
        try
        {
            
            MailMessage mail = new MailMessage();
            mail.Subject = "EMAIL ENVIADO PELO FALE CONOSCO";
            mail.IsBodyHtml = false;
            mail.To.Add("[email protected]");
            MailAddress from = new MailAddress("[email protected]");
            mail.From = from;
            mail.Body = "MENSAGEM ENVIADA PELO USUÁRIO \n";
            mail.Body += "Nome: " + Nome.Text + "\n";
            mail.Body += "Email: " + Email.Text + "\n";
            mail.Body += "Mensagem: " + Mensagem.Text + "\n";
            //2. CONECTAR AO SERVIDOR DE EMAIL E ENVIAR
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.seudominio.com.br";
            smtp.Port = 587;
            smtp.EnableSsl = false;
            smtp.Credentials = new NetworkCredential("[email protected]", "suasenha");
            smtp.Send(mail);
        }
        catch (Exception ex)
        {
            Erro.Text = "Houve uma falha inesperada ao enviar o e-mail. <br>";
            string conteudo = "Data         : " + DateTime.Now.ToString() + "\n" ;
            conteudo += "Mensagem       : " + ex.Message + "\n";
            //Dicas: procure respostas em http://msdn.com.br
            //Está no ex (Exception do catch)
            conteudo += "Arquivo        : " + "..." + "\n";
            conteudo += "Método         : " + "..." + "\n";
            conteudo += "Linha do Erro  : " + ex.StackTrace + "\n";
            //Está em outro lugar
            HttpBrowserCapabilities browser = HttpContext.Current.Request.Browser;
            conteudo += "Browser        : " + browser.Browser + " - " + browser.Version + "\n";
            string host = Dns.GetHostName();
            IPAddress[] ip = Dns.GetHostAddresses(host);
            conteudo += "IP             : " + ip[5].ToString() + "\n";
            conteudo += "--------------------------------------------------\n";
            //DEFINE O CAMINHO ONDE O ARQUIVO SERA GRAVADO.
            string caminhoVirtual = "~/Falhas/Log.txt";
            string caminhoFisico = HttpContext.Current.Server.MapPath(caminhoVirtual);
            //grava a exceção no arquivo log.txt
            File.AppendAllText(caminhoFisico, conteudo);
                           
        }
Hello Lucas, I played your code and fixed my problem. Thank you very much!
– Gabriel Errera