On any given day, more than 4,000 people pass through the library at California State University-Los Angeles.

On April 11, one of them had measles. The building has only one entrance, which means that anyone who entered or exited the library within two hours of that person’s visit potentially was exposed to one of the most contagious diseases on Earth.

It’s the stuff of public health nightmares: Everyone at the library between 11 a.m. and 3 p.m. that day had to be identified, warned and possibly quarantined. Measles is so contagious that up to 90% of people close to an infected person who are not protected by a vaccine or previous case of the disease will become infected. But how could the university figure out who had been in the library during that time frame? And which of those people were vulnerable to infection?

Rooting out answers to such questions is the job of the public health detectives who work at health departments across the country.

In 2000, the United States declared the measles eradicated, thanks to widespread use of vaccines. But the virulent disease is back, with more than 1000 cases confirmed nationwide this year through June 3 — the greatest number since 1992. And public health departments are redirecting scarce resources to try to control the spread.

Using basic techniques in place for over 100 years, public health investigators work to control an outbreak before it balloons. Such investigations have evolved with new technologies but remain among the best defenses against infectious disease outbreaks — and among the great untold costs of an epidemic.

The New York City Department of Health and Mental Hygiene, which has confirmed 550 measles cases since September, has spent more than $2.3 million on related investigations. The Los Angeles County Department of Public Health estimates spending as much as $2,000 to track down each contact of a confirmed patient — and it has made hundreds of such efforts in recent months.

“Public health departments across the country have had their budgets tightened in a sustained fashion over the past 15 years,” said Dr. William Schaffner, an infectious-disease specialist at Vanderbilt University. “There are no public health departments that are like firemen playing pinochle and waiting for an outbreak. They have other things to do, and they have to put aside those tasks to deal with an outbreak.”

At Cal State LA, public health officials visited the library and tried to figure out exactly where the infected student had gone — the photocopy area, for example — to determine who might have been exposed. They worked with the school to identify which library employees were present. They scoured library records to find anyone who had checked out books or logged onto a library computer during the specified time period.

But they realized they were missing others who may have come in to browse, work or eat at a library cafe. So, school officials sent out emails and posted on Facebook, Twitter and Instagram to ask anyone who may have been at the library to come forward.

Working together, Cal State and county health officials came up with a list of 1,094 people who were exposed; all were required to present proof they had been vaccinated or had immunity.

At one point during the investigation, 887 people were under a blanket quarantine order from the Los Angeles County Department of Public Health until they could establish their immunity status.

Public health departments regularly employ this sort of shoe-leather detective work to track and control outbreaks of sexually transmitted diseases and foodborne illnesses like salmonella. But there is a palpable sense of frustration that sets the measles apart: It is easily preventable.

“We shouldn’t have to be using these ancient techniques. We should have everybody immunized,” said Dr. Alan Melnick, director of public health in Clark County, Wash., which logged 71 measles cases during a two-month outbreak that ended in February. “That’s what keeps me up at night. If we stop vaccinating, we can turn the clock back to the Middle Ages.”

In the course of Clark County’s outbreak, 237 people spent 19,071 hours doing outreach, investigation and monitoring the health of people exposed, at a cost of more than $864,000. They investigated 53 exposure sites, including 15 schools and the arena where the Portland Trail Blazers play just across the state line in Oregon.

All this work meant delays in other programs, including restaurant health inspections and a home-visit program for high-risk pregnant women and infants.

“Just because the measles outbreak is going on, it doesn’t mean other communicable diseases are taking a holiday,” Melnick said.

There was a time public health officials wouldn’t have bothered. Before the late 19th century, officials “were busy trying to control diseases like typhoid, cholera and smallpox,” which had much higher death rates than measles, said Graham Mooney, an associate professor at Johns Hopkins University who studies the history of medicine. But as other infectious diseases declined, officials focused more intently on measles.

By the early 20th century, schools began noting which students had already had the measles, and who might be vulnerable. When a child fell ill, he might be sent home with a card to be signed by a physician before he could return. A school medical inspector often would visit the home to make sure the child remained isolated.

During an epidemic involving thousands of cases, officials placed warnings in newspapers and later on the radio, describing likely symptoms and asking parents to keep sick children at home. “Now it’s Twitter; before, it would have been The Baltimore Sun or the Chicago Tribune or the L.A. Times. But the actual information may not have changed,” said Mooney.

Today, public health departments almost invariably learn of a measles case through a health care provider. Measles is a reportable disease, which means that any provider who suspects a case has to warn local health officials. Someone from the department visits the patient to conduct an interview and determine precisely where they might have gone while contagious. For the measles, that’s four days before the rash appears, and four days after, for a total of nine days.

The interviews are rigorous. “We have to assess their hangouts, their friends, their hobbies, which grocery store do they go to, do they take Lyft or Uber?” explained L.A. County public health nurse Adarsh Almalvez.

For most people, it’s hard to remember everywhere they went days earlier. Some patients are reluctant to share details. Almalvez said she starts by building a rapport, asking them about their favorite foods and where they get their hair cut. She said it’s crucial to get the patient’s cooperation.

She looks for clues around the house that could tell her who else might be living there. Extra pairs of shoes in the hallway, for example, might indicate other residents. If she’s interviewing a woman and finds the toilet seat up, she knows a man likely has been there as well.

The goal is to come away with a list of all possible contacts and locations the patient visited while contagious. The results can read like a bizarrely intimate window into one person’s day. L.A. County recently published a patient’s itinerary in April; in one enviable day, that patient visited Peet’s Coffee, Fratelli Café, TART restaurant, The Grove, the Los Angeles Farmers Market, Whole Foods and the La Brea Tar Pits.

Public health officials visit each site to gather more information. They reach out to ride-sharing services to locate drivers and other passengers who might have been in the same car during the infectious period. At restaurants, employees are easily identified, but customers can be hard to find. Officials don’t routinely look at surveillance video or track down people through credit card receipts. Instead, they mainly rely on news releases and social media to spread the word. They also look to schools and businesses to do outreach.

This can be a lot of work, especially for medical clinics where a measles patient initially sought care. During a 2017 outbreak, Children’s Minnesota, a hospital system in the Twin Cities, spent $300,000 on their emergency response. Part of that was tracking down everyone in the waiting room within two hours of a measles patient.

Patsy Stinchfield, who directs the Children’s Minnesota’s infection prevention and control program, has worked on three measles outbreaks, in 1989, 2011 and 2017. She said the work has gotten more efficient because of electronic health records and the state’s electronic vaccination registry. With the click of a few buttons, investigators can determine who was in the waiting room with a measles patient, and which people were unvaccinated.

Still, Stinchfield said, measles outbreaks remain a source of great frustration. “If we can get people to use the [measles] vaccine, we won’t have to spend all these health care dollars, all of this time and energy on follow-up,” she said. “And we won’t have to have all these miserable, sick children.”

Kaiser Health News (KHN) is a nonprofit news service covering health issues. It is an editorially independent program of the Kaiser Family Foundation that is not affiliated with Kaiser Permanente.

FOLLOW NBC HEALTH ON TWITTER & FACEBOOK